| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // MOSignExtras.m
- //
- // Created by SuperCabbage on 2024/5/31
- // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
- //
- #import "MOSignExtras.h"
- #import "MOSignPrizes.h"
- NSString *const kMOSignExtrasDay = @"day";
- NSString *const kMOSignExtrasGive = @"give";
- NSString *const kMOSignExtrasSignPrizes = @"prizes";
- @interface MOSignExtras ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOSignExtras
- @synthesize day = _day;
- @synthesize give = _give;
- @synthesize signPrizes = _signPrizes;
- + (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.day = [[self objectOrNilForKey:kMOSignExtrasDay fromDictionary:dict] doubleValue];
- self.give = [[self objectOrNilForKey:kMOSignExtrasGive fromDictionary:dict] boolValue];
- NSObject *receivedMOSignPrizes = [dict objectForKey:kMOSignExtrasSignPrizes];
- NSMutableArray *parsedMOSignPrizes = [NSMutableArray array];
-
- if ([receivedMOSignPrizes isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOSignPrizes) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOSignPrizes addObject:[MOSignPrizes modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOSignPrizes isKindOfClass:[NSDictionary class]]) {
- [parsedMOSignPrizes addObject:[MOSignPrizes modelObjectWithDictionary:(NSDictionary *)receivedMOSignPrizes]];
- }
- self.signPrizes = [NSArray arrayWithArray:parsedMOSignPrizes];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- [mutableDict setValue:[NSNumber numberWithDouble:self.day] forKey:kMOSignExtrasDay];
- [mutableDict setValue:[NSNumber numberWithBool:self.give] forKey:kMOSignExtrasGive];
- NSMutableArray *tempArrayForSignPrizes = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.signPrizes) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForSignPrizes addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForSignPrizes addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForSignPrizes] forKey:kMOSignExtrasSignPrizes];
- 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.day = [aDecoder decodeDoubleForKey:kMOSignExtrasDay];
- self.give = [aDecoder decodeBoolForKey:kMOSignExtrasGive];
- self.signPrizes = [aDecoder decodeObjectForKey:kMOSignExtrasSignPrizes];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeDouble:_day forKey:kMOSignExtrasDay];
- [aCoder encodeBool:_give forKey:kMOSignExtrasGive];
- [aCoder encodeObject:_signPrizes forKey:kMOSignExtrasSignPrizes];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOSignExtras *copy = [[MOSignExtras alloc] init];
-
-
-
- if (copy) {
- copy.day = self.day;
- copy.give = self.give;
- copy.signPrizes = [self.signPrizes copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|