MOBannerData.m 3.1 KB

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