MOJumpList.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // MOJumpList.m
  3. //
  4. // Created by SuperCabbage on 2024/3/20
  5. // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOJumpList.h"
  8. NSString *const kMOJumpListImg = @"img";
  9. NSString *const kMOJumpListJump = @"jump";
  10. NSString *const kMOJumpListTarget = @"target";
  11. NSString *const kMOJumpListTitle = @"title";
  12. NSString *const kMOJumpListIcon = @"icon";
  13. NSString *const kMOJumpListEndTime = @"endTime";
  14. @interface MOJumpList ()
  15. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  16. @end
  17. @implementation MOJumpList
  18. @synthesize img = _img;
  19. @synthesize jump = _jump;
  20. @synthesize target = _target;
  21. @synthesize title = _title;
  22. @synthesize icon = _icon;
  23. @synthesize endTime = _endTime;
  24. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  25. return [[self alloc] initWithDictionary:dict];
  26. }
  27. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  28. self = [super init];
  29. // This check serves to make sure that a non-NSDictionary object
  30. // passed into the model class doesn't break the parsing.
  31. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  32. self.img = [self objectOrNilForKey:kMOJumpListImg fromDictionary:dict];
  33. self.jump = [self objectOrNilForKey:kMOJumpListJump fromDictionary:dict];
  34. self.target = [[self objectOrNilForKey:kMOJumpListTarget fromDictionary:dict] doubleValue];
  35. self.title = [self objectOrNilForKey:kMOJumpListTitle fromDictionary:dict];
  36. self.icon = [self objectOrNilForKey:kMOJumpListIcon fromDictionary:dict];
  37. self.endTime = [[self objectOrNilForKey:kMOJumpListEndTime fromDictionary:dict] doubleValue];
  38. }
  39. return self;
  40. }
  41. - (NSDictionary *)dictionaryRepresentation {
  42. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  43. [mutableDict setValue:self.img forKey:kMOJumpListImg];
  44. [mutableDict setValue:self.jump forKey:kMOJumpListJump];
  45. [mutableDict setValue:[NSNumber numberWithDouble:self.target] forKey:kMOJumpListTarget];
  46. [mutableDict setValue:self.title forKey:kMOJumpListTitle];
  47. [mutableDict setValue:self.icon forKey:kMOJumpListIcon];
  48. [mutableDict setValue:[NSNumber numberWithDouble:self.endTime] forKey:kMOJumpListEndTime];
  49. return [NSDictionary dictionaryWithDictionary:mutableDict];
  50. }
  51. - (NSString *)description {
  52. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  53. }
  54. #pragma mark - Helper Method
  55. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  56. id object = [dict objectForKey:aKey];
  57. return [object isEqual:[NSNull null]] ? nil : object;
  58. }
  59. #pragma mark - NSCoding Methods
  60. - (id)initWithCoder:(NSCoder *)aDecoder {
  61. self = [super init];
  62. self.img = [aDecoder decodeObjectForKey:kMOJumpListImg];
  63. self.jump = [aDecoder decodeObjectForKey:kMOJumpListJump];
  64. self.target = [aDecoder decodeDoubleForKey:kMOJumpListTarget];
  65. self.title = [aDecoder decodeObjectForKey:kMOJumpListTitle];
  66. self.icon = [aDecoder decodeObjectForKey:kMOJumpListIcon];
  67. self.endTime = [aDecoder decodeDoubleForKey:kMOJumpListEndTime];
  68. return self;
  69. }
  70. - (void)encodeWithCoder:(NSCoder *)aCoder
  71. {
  72. [aCoder encodeObject:_img forKey:kMOJumpListImg];
  73. [aCoder encodeObject:_jump forKey:kMOJumpListJump];
  74. [aCoder encodeDouble:_target forKey:kMOJumpListTarget];
  75. [aCoder encodeObject:_title forKey:kMOJumpListTitle];
  76. [aCoder encodeObject:_icon forKey:kMOJumpListIcon];
  77. [aCoder encodeDouble:_endTime forKey:kMOJumpListEndTime];
  78. }
  79. - (id)copyWithZone:(NSZone *)zone {
  80. MOJumpList *copy = [[MOJumpList alloc] init];
  81. if (copy) {
  82. copy.img = [self.img copyWithZone:zone];
  83. copy.jump = [self.jump copyWithZone:zone];
  84. copy.target = self.target;
  85. copy.title = [self.title copyWithZone:zone];
  86. copy.icon = [self.icon copyWithZone:zone];
  87. copy.endTime = self.endTime;
  88. }
  89. return copy;
  90. }
  91. @end