MOBaseVips.m 3.6 KB

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