| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- //
- // MOSvgaBaseData.m
- //
- // Created by SuperCabbage on 2023/12/20
- // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
- //
- #import "MOSvgaBaseData.h"
- #import "MOEffect.h"
- NSString *const kMOSvgaBaseDataNext = @"next";
- NSString *const kMOSvgaBaseDataEffectList = @"list";
- @interface MOSvgaBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOSvgaBaseData
- @synthesize next = _next;
- @synthesize effectList = _effectList;
- + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
- return [[self alloc] initWithDictionary:dict];
- }
- - (instancetype)initWithDictionary:(NSDictionary *)dict {
- self = [super init];
-
- // This check serves to make sure that a non-NSDictionary object
- // passed into the model class doesn't break the parsing.
- if (self && [dict isKindOfClass:[NSDictionary class]]) {
- self.next = [self objectOrNilForKey:kMOSvgaBaseDataNext fromDictionary:dict];
- NSObject *receivedMOEffect = [dict objectForKey:kMOSvgaBaseDataEffectList];
- NSMutableArray *parsedMOEffect = [NSMutableArray array];
-
- if ([receivedMOEffect isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOEffect) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOEffect isKindOfClass:[NSDictionary class]]) {
- [parsedMOEffect addObject:[MOEffect modelObjectWithDictionary:(NSDictionary *)receivedMOEffect]];
- }
- self.effectList = [NSArray arrayWithArray:parsedMOEffect];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:self.next forKey:kMOSvgaBaseDataNext];
- NSMutableArray *tempArrayForEffect = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.effectList) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForEffect addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForEffect addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForEffect] forKey:kMOSvgaBaseDataEffectList];
- return [NSDictionary dictionaryWithDictionary:mutableDict];
- }
- - (NSString *)description {
- return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
- }
- #pragma mark - Helper Method
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
- id object = [dict objectForKey:aKey];
- return [object isEqual:[NSNull null]] ? nil : object;
- }
- #pragma mark - NSCoding Methods
- - (id)initWithCoder:(NSCoder *)aDecoder {
- self = [super init];
- self.next = [aDecoder decodeObjectForKey:kMOSvgaBaseDataNext];
- self.effectList = [aDecoder decodeObjectForKey:kMOSvgaBaseDataEffectList];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_next forKey:kMOSvgaBaseDataNext];
- [aCoder encodeObject:_effectList forKey:kMOSvgaBaseDataEffectList];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOSvgaBaseData *copy = [[MOSvgaBaseData alloc] init];
-
-
-
- if (copy) {
- copy.next = [self.next copyWithZone:zone];
- copy.effectList = [self.effectList copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|