MODialogsData.m 3.3 KB

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