MOSvgaBaseData.m 3.5 KB

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