// // MOGiftListBaseData.m // // Created by SuperCabbage on 2023/11/24 // Copyright (c) 2023 __MyCompanyName__. All rights reserved. // #import "MOGiftListBaseData.h" #import "MOGiftlist.h" NSString *const kMOGiftListBaseDataGiftlist = @"list"; NSString *const kMOGiftListBaseDataNext = @"next"; NSString *const kMOGiftListBaseDataGiftRedCount = @"giftRedCount"; NSString *const kMOGiftListBaseDataPropRedCount = @"propRedCount"; NSString *const kMOGiftListBaseDataPropNearExpiryPropCount = @"propNearExpiryPropCount"; @interface MOGiftListBaseData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOGiftListBaseData @synthesize giftlist = _giftlist; @synthesize next = _next; @synthesize giftRedCount = _giftRedCount; @synthesize propRedCount = _propRedCount; @synthesize propNearExpiryPropCount = _propNearExpiryPropCount; + (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]]) { NSObject *receivedMOGiftlist = [dict objectForKey:kMOGiftListBaseDataGiftlist]; NSMutableArray *parsedMOGiftlist = [NSMutableArray array]; if ([receivedMOGiftlist isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOGiftlist) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOGiftlist addObject:[MOGiftlist modelObjectWithDictionary:item]]; } } } else if ([receivedMOGiftlist isKindOfClass:[NSDictionary class]]) { [parsedMOGiftlist addObject:[MOGiftlist modelObjectWithDictionary:(NSDictionary *)receivedMOGiftlist]]; } self.giftlist = [NSArray arrayWithArray:parsedMOGiftlist]; self.next = [self objectOrNilForKey:kMOGiftListBaseDataNext fromDictionary:dict]; self.giftRedCount = [[self objectOrNilForKey:kMOGiftListBaseDataGiftRedCount fromDictionary:dict] doubleValue]; self.propRedCount = [[self objectOrNilForKey:kMOGiftListBaseDataPropRedCount fromDictionary:dict] doubleValue]; self.propNearExpiryPropCount = [[self objectOrNilForKey:kMOGiftListBaseDataPropNearExpiryPropCount fromDictionary:dict] doubleValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; NSMutableArray *tempArrayForGiftlist = [NSMutableArray array]; for (NSObject *subArrayObject in self.giftlist) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForGiftlist addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForGiftlist addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftlist] forKey:kMOGiftListBaseDataGiftlist]; [mutableDict setValue:self.next forKey:kMOGiftListBaseDataNext]; [mutableDict setValue:[NSNumber numberWithDouble:self.giftRedCount] forKey:kMOGiftListBaseDataGiftRedCount]; [mutableDict setValue:[NSNumber numberWithDouble:self.propRedCount] forKey:kMOGiftListBaseDataPropRedCount]; [mutableDict setValue:[NSNumber numberWithDouble:self.propNearExpiryPropCount] forKey:kMOGiftListBaseDataPropNearExpiryPropCount]; 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.giftlist = [aDecoder decodeObjectForKey:kMOGiftListBaseDataGiftlist]; self.next = [aDecoder decodeObjectForKey:kMOGiftListBaseDataNext]; self.giftRedCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataGiftRedCount]; self.propRedCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataPropRedCount]; self.propNearExpiryPropCount = [aDecoder decodeDoubleForKey:kMOGiftListBaseDataPropNearExpiryPropCount]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_giftlist forKey:kMOGiftListBaseDataGiftlist]; [aCoder encodeObject:_next forKey:kMOGiftListBaseDataNext]; [aCoder encodeDouble:_giftRedCount forKey:kMOGiftListBaseDataGiftRedCount]; [aCoder encodeDouble:_propRedCount forKey:kMOGiftListBaseDataPropRedCount]; [aCoder encodeDouble:_propNearExpiryPropCount forKey:kMOGiftListBaseDataPropNearExpiryPropCount]; } - (id)copyWithZone:(NSZone *)zone { MOGiftListBaseData *copy = [[MOGiftListBaseData alloc] init]; if (copy) { copy.giftlist = [self.giftlist copyWithZone:zone]; copy.next = [self.next copyWithZone:zone]; copy.giftRedCount = self.giftRedCount; copy.propRedCount = self.propRedCount; copy.propNearExpiryPropCount = self.propNearExpiryPropCount; } return copy; } @end