// // MOGiftBaseData.m // // Created by SuperCabbage on 2023/11/22 // Copyright (c) 2023 __MyCompanyName__. All rights reserved. // #import "MOGiftBaseData.h" #import "MOWishList.h" #import "MOGiftWishBase.h" NSString *const kMOGiftBaseDataNext = @"next"; NSString *const kMOGiftBaseDataWishList = @"list"; NSString *const kMOGiftBaseDataComplete = @"complete"; NSString *const kMOGiftBaseDataGiftWishBase = @"ongoing"; @interface MOGiftBaseData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOGiftBaseData @synthesize next = _next; @synthesize wishList = _wishList; @synthesize complete = _complete; @synthesize giftWishBase = _giftWishBase; + (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:kMOGiftBaseDataNext fromDictionary:dict]; NSObject *receivedMOWishList = [dict objectForKey:kMOGiftBaseDataWishList]; NSMutableArray *parsedMOWishList = [NSMutableArray array]; if ([receivedMOWishList isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOWishList) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOWishList addObject:[MOWishList modelObjectWithDictionary:item]]; } } } else if ([receivedMOWishList isKindOfClass:[NSDictionary class]]) { [parsedMOWishList addObject:[MOWishList modelObjectWithDictionary:(NSDictionary *)receivedMOWishList]]; } self.wishList = [NSArray arrayWithArray:parsedMOWishList]; self.complete = [[self objectOrNilForKey:kMOGiftBaseDataComplete fromDictionary:dict] doubleValue]; NSObject *receivedMOGiftWishBase = [dict objectForKey:kMOGiftBaseDataGiftWishBase]; NSMutableArray *parsedMOGiftWishBase = [NSMutableArray array]; if ([receivedMOGiftWishBase isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMOGiftWishBase) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMOGiftWishBase addObject:[MOGiftWishBase modelObjectWithDictionary:item]]; } } } else if ([receivedMOGiftWishBase isKindOfClass:[NSDictionary class]]) { [parsedMOGiftWishBase addObject:[MOGiftWishBase modelObjectWithDictionary:(NSDictionary *)receivedMOGiftWishBase]]; } self.giftWishBase = [NSArray arrayWithArray:parsedMOGiftWishBase]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:self.next forKey:kMOGiftBaseDataNext]; NSMutableArray *tempArrayForWishList = [NSMutableArray array]; for (NSObject *subArrayObject in self.wishList) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForWishList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForWishList addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForWishList] forKey:kMOGiftBaseDataWishList]; [mutableDict setValue:[NSNumber numberWithDouble:self.complete] forKey:kMOGiftBaseDataComplete]; NSMutableArray *tempArrayForGiftWishBase = [NSMutableArray array]; for (NSObject *subArrayObject in self.giftWishBase) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForGiftWishBase addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForGiftWishBase addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftWishBase] forKey:kMOGiftBaseDataGiftWishBase]; 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:kMOGiftBaseDataNext]; self.wishList = [aDecoder decodeObjectForKey:kMOGiftBaseDataWishList]; self.complete = [aDecoder decodeDoubleForKey:kMOGiftBaseDataComplete]; self.giftWishBase = [aDecoder decodeObjectForKey:kMOGiftBaseDataGiftWishBase]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:_next forKey:kMOGiftBaseDataNext]; [aCoder encodeObject:_wishList forKey:kMOGiftBaseDataWishList]; [aCoder encodeDouble:_complete forKey:kMOGiftBaseDataComplete]; [aCoder encodeObject:_giftWishBase forKey:kMOGiftBaseDataGiftWishBase]; } - (id)copyWithZone:(NSZone *)zone { MOGiftBaseData *copy = [[MOGiftBaseData alloc] init]; if (copy) { copy.next = [self.next copyWithZone:zone]; copy.wishList = [self.wishList copyWithZone:zone]; copy.complete = self.complete; copy.giftWishBase = [self.giftWishBase copyWithZone:zone]; } return copy; } @end