| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // 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
|