| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // MOShopBaseData.m
- //
- // Created by SuperCabbage on 2023/12/17
- // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
- //
- #import "MOShopBaseData.h"
- #import "MOShopList.h"
- NSString *const kMOShopBaseDataNext = @"next";
- NSString *const kMOShopBaseDataShopList = @"list";
- NSString *const kMOShopBaseDataGiftRedCount = @"giftRedCount";
- NSString *const kMOShopBaseDataPropRedCount = @"propRedCount";
- NSString *const kMOShopBaseDataPropNearExpiryPropCount = @"propNearExpiryPropCount";
- @interface MOShopBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOShopBaseData
- @synthesize next = _next;
- @synthesize shopList = _shopList;
- @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]]) {
- self.next = [self objectOrNilForKey:kMOShopBaseDataNext fromDictionary:dict];
- NSObject *receivedMOShopList = [dict objectForKey:kMOShopBaseDataShopList];
- NSMutableArray *parsedMOShopList = [NSMutableArray array];
-
- if ([receivedMOShopList isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOShopList) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOShopList addObject:[MOShopList modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOShopList isKindOfClass:[NSDictionary class]]) {
- [parsedMOShopList addObject:[MOShopList modelObjectWithDictionary:(NSDictionary *)receivedMOShopList]];
- }
- self.shopList = [NSArray arrayWithArray:parsedMOShopList];
- self.giftRedCount = [[self objectOrNilForKey:kMOShopBaseDataGiftRedCount fromDictionary:dict] doubleValue];
- self.propRedCount = [[self objectOrNilForKey:kMOShopBaseDataPropRedCount fromDictionary:dict] doubleValue];
- self.propNearExpiryPropCount = [[self objectOrNilForKey:kMOShopBaseDataPropNearExpiryPropCount fromDictionary:dict] doubleValue];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:self.next forKey:kMOShopBaseDataNext];
- NSMutableArray *tempArrayForShopList = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.shopList) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForShopList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForShopList addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForShopList] forKey:kMOShopBaseDataShopList];
-
- [mutableDict setValue:[NSNumber numberWithDouble:self.giftRedCount] forKey:kMOShopBaseDataGiftRedCount];
- [mutableDict setValue:[NSNumber numberWithDouble:self.propRedCount] forKey:kMOShopBaseDataPropRedCount];
- [mutableDict setValue:[NSNumber numberWithDouble:self.propNearExpiryPropCount] forKey:kMOShopBaseDataPropNearExpiryPropCount];
- 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:kMOShopBaseDataNext];
- self.shopList = [aDecoder decodeObjectForKey:kMOShopBaseDataShopList];
-
- self.giftRedCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataGiftRedCount];
- self.propRedCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataPropRedCount];
- self.propNearExpiryPropCount = [aDecoder decodeDoubleForKey:kMOShopBaseDataPropNearExpiryPropCount];
-
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_next forKey:kMOShopBaseDataNext];
- [aCoder encodeObject:_shopList forKey:kMOShopBaseDataShopList];
-
- [aCoder encodeDouble:_giftRedCount forKey:kMOShopBaseDataGiftRedCount];
- [aCoder encodeDouble:_propRedCount forKey:kMOShopBaseDataPropRedCount];
- [aCoder encodeDouble:_propNearExpiryPropCount forKey:kMOShopBaseDataPropNearExpiryPropCount];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOShopBaseData *copy = [[MOShopBaseData alloc] init];
-
-
-
- if (copy) {
- copy.next = [self.next copyWithZone:zone];
- copy.shopList = [self.shopList copyWithZone:zone];
-
- copy.giftRedCount = self.giftRedCount;
- copy.propRedCount = self.propRedCount;
- copy.propNearExpiryPropCount = self.propNearExpiryPropCount;
- }
-
- return copy;
- }
- @end
|