// // MOReceivingBaseData.m // // Created by SuperCabbage on 2024/6/19 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MOReceivingBaseData.h" #import "MOReceivingInfo.h" NSString *const kMOReceivingBaseDataReceivedAmount = @"receivedAmount"; NSString *const kMOReceivingBaseDataNext = @"next"; NSString *const kMOReceivingBaseDataWalletDiamonds = @"walletDiamonds"; NSString *const kMOReceivingBaseDataReceivingInfo = @"receivingInfo"; NSString *const kMOReceivingBaseDataFirstTime = @"firstTime"; NSString *const kMOReceivingBaseDataLastReceivedNum = @"lastReceivedNum"; @interface MOReceivingBaseData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MOReceivingBaseData @synthesize receivedAmount = _receivedAmount; @synthesize next = _next; @synthesize walletDiamonds = _walletDiamonds; @synthesize receivingInfo = _receivingInfo; @synthesize firstTime = _firstTime; @synthesize lastReceivedNum = _lastReceivedNum; + (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.receivedAmount = [[self objectOrNilForKey:kMOReceivingBaseDataReceivedAmount fromDictionary:dict] doubleValue]; self.next = [self objectOrNilForKey:kMOReceivingBaseDataNext fromDictionary:dict]; self.walletDiamonds = [[self objectOrNilForKey:kMOReceivingBaseDataWalletDiamonds fromDictionary:dict] doubleValue]; self.receivingInfo = [MOReceivingInfo modelObjectWithDictionary:[dict objectForKey:kMOReceivingBaseDataReceivingInfo]]; self.firstTime = [[self objectOrNilForKey:kMOReceivingBaseDataFirstTime fromDictionary:dict] boolValue]; self.lastReceivedNum = [[self objectOrNilForKey:kMOReceivingBaseDataLastReceivedNum fromDictionary:dict] doubleValue]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithDouble:self.receivedAmount] forKey:kMOReceivingBaseDataReceivedAmount]; [mutableDict setValue:self.next forKey:kMOReceivingBaseDataNext]; [mutableDict setValue:[NSNumber numberWithDouble:self.walletDiamonds] forKey:kMOReceivingBaseDataWalletDiamonds]; [mutableDict setValue:[self.receivingInfo dictionaryRepresentation] forKey:kMOReceivingBaseDataReceivingInfo]; [mutableDict setValue:[NSNumber numberWithBool:self.firstTime] forKey:kMOReceivingBaseDataFirstTime]; [mutableDict setValue:[NSNumber numberWithDouble:self.lastReceivedNum] forKey:kMOReceivingBaseDataLastReceivedNum]; 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.receivedAmount = [aDecoder decodeDoubleForKey:kMOReceivingBaseDataReceivedAmount]; self.next = [aDecoder decodeObjectForKey:kMOReceivingBaseDataNext]; self.walletDiamonds = [aDecoder decodeDoubleForKey:kMOReceivingBaseDataWalletDiamonds]; self.receivingInfo = [aDecoder decodeObjectForKey:kMOReceivingBaseDataReceivingInfo]; self.firstTime = [aDecoder decodeBoolForKey:kMOReceivingBaseDataFirstTime]; self.lastReceivedNum = [aDecoder decodeDoubleForKey:kMOReceivingBaseDataLastReceivedNum]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeDouble:_receivedAmount forKey:kMOReceivingBaseDataReceivedAmount]; [aCoder encodeObject:_next forKey:kMOReceivingBaseDataNext]; [aCoder encodeDouble:_walletDiamonds forKey:kMOReceivingBaseDataWalletDiamonds]; [aCoder encodeObject:_receivingInfo forKey:kMOReceivingBaseDataReceivingInfo]; [aCoder encodeBool:_firstTime forKey:kMOReceivingBaseDataFirstTime]; [aCoder encodeDouble:_lastReceivedNum forKey:kMOReceivingBaseDataLastReceivedNum]; } - (id)copyWithZone:(NSZone *)zone { MOReceivingBaseData *copy = [[MOReceivingBaseData alloc] init]; if (copy) { copy.receivedAmount = self.receivedAmount; copy.next = [self.next copyWithZone:zone]; copy.walletDiamonds = self.walletDiamonds; copy.receivingInfo = [self.receivingInfo copyWithZone:zone]; copy.firstTime = self.firstTime; copy.lastReceivedNum = self.lastReceivedNum; } return copy; } @end