| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // MOGiftPackList.m
- //
- // Created by SuperCabbage on 2024/9/10
- // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
- //
- #import "MOGiftPackList.h"
- #import "MOGiftPackItems.h"
- NSString *const kMOGiftPackListAmount = @"amount";
- NSString *const kMOGiftPackListId = @"id";
- NSString *const kMOGiftPackListCostPrice = @"costPrice";
- NSString *const kMOGiftPackListGiftPackItems = @"items";
- NSString *const kMOGiftPackListBought = @"bought";
- NSString *const kMOGiftPackListPayCode = @"payCode";
- @interface MOGiftPackList ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOGiftPackList
- @synthesize amount = _amount;
- @synthesize id = _id;
- @synthesize costPrice = _costPrice;
- @synthesize giftPackItems = _giftPackItems;
- @synthesize bought = _bought;
- @synthesize payCode = _payCode;
- + (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.amount = [[self objectOrNilForKey:kMOGiftPackListAmount fromDictionary:dict] doubleValue];
- self.id = [self objectOrNilForKey:kMOGiftPackListId fromDictionary:dict];
- self.costPrice = [[self objectOrNilForKey:kMOGiftPackListCostPrice fromDictionary:dict] doubleValue];
- NSObject *receivedMOGiftPackItems = [dict objectForKey:kMOGiftPackListGiftPackItems];
- NSMutableArray *parsedMOGiftPackItems = [NSMutableArray array];
-
- if ([receivedMOGiftPackItems isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOGiftPackItems) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOGiftPackItems addObject:[MOGiftPackItems modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOGiftPackItems isKindOfClass:[NSDictionary class]]) {
- [parsedMOGiftPackItems addObject:[MOGiftPackItems modelObjectWithDictionary:(NSDictionary *)receivedMOGiftPackItems]];
- }
- self.giftPackItems = [NSArray arrayWithArray:parsedMOGiftPackItems];
- self.bought = [[self objectOrNilForKey:kMOGiftPackListBought fromDictionary:dict] boolValue];
-
- self.payCode = [self objectOrNilForKey:kMOGiftPackListPayCode fromDictionary:dict];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:[NSNumber numberWithDouble:self.amount] forKey:kMOGiftPackListAmount];
- [mutableDict setValue:self.id forKey:kMOGiftPackListId];
- [mutableDict setValue:[NSNumber numberWithDouble:self.costPrice] forKey:kMOGiftPackListCostPrice];
- NSMutableArray *tempArrayForGiftPackItems = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.giftPackItems) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForGiftPackItems addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForGiftPackItems addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForGiftPackItems] forKey:kMOGiftPackListGiftPackItems];
- [mutableDict setValue:[NSNumber numberWithBool:self.bought] forKey:kMOGiftPackListBought];
-
- [mutableDict setValue:self.payCode forKey:kMOGiftPackListPayCode];
- 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.amount = [aDecoder decodeDoubleForKey:kMOGiftPackListAmount];
- self.id = [aDecoder decodeObjectForKey:kMOGiftPackListId];
- self.costPrice = [aDecoder decodeDoubleForKey:kMOGiftPackListCostPrice];
- self.giftPackItems = [aDecoder decodeObjectForKey:kMOGiftPackListGiftPackItems];
- self.bought = [aDecoder decodeBoolForKey:kMOGiftPackListBought];
-
- self.payCode = [aDecoder decodeObjectForKey:kMOGiftPackListPayCode];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeDouble:_amount forKey:kMOGiftPackListAmount];
- [aCoder encodeObject:_id forKey:kMOGiftPackListId];
- [aCoder encodeDouble:_costPrice forKey:kMOGiftPackListCostPrice];
- [aCoder encodeObject:_giftPackItems forKey:kMOGiftPackListGiftPackItems];
- [aCoder encodeBool:_bought forKey:kMOGiftPackListBought];
-
- [aCoder encodeObject:_payCode forKey:kMOGiftPackListPayCode];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOGiftPackList *copy = [[MOGiftPackList alloc] init];
-
-
-
- if (copy) {
- copy.amount = self.amount;
- copy.id = [self.id copyWithZone:zone];
- copy.costPrice = self.costPrice;
- copy.giftPackItems = [self.giftPackItems copyWithZone:zone];
- copy.bought = self.bought;
-
- copy.payCode = [self.payCode copyWithZone:zone];
-
- copy.isChoose = self.isChoose;
- }
-
- return copy;
- }
- @end
|