// // MORedConfigData.m // // Created by SuperCabbage on 2024/6/14 // Copyright (c) 2024 __MyCompanyName__. All rights reserved. // #import "MORedConfigData.h" #import "MORedSendConfigs.h" NSString *const kMORedConfigDataRefundMinutes = @"refundMinutes"; NSString *const kMORedConfigDataStartDelayMinutes = @"startDelayMinutes"; NSString *const kMORedConfigDataRedSendConfigs = @"sendConfigs"; @interface MORedConfigData () - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict; @end @implementation MORedConfigData @synthesize refundMinutes = _refundMinutes; @synthesize startDelayMinutes = _startDelayMinutes; @synthesize redSendConfigs = _redSendConfigs; + (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.refundMinutes = [[self objectOrNilForKey:kMORedConfigDataRefundMinutes fromDictionary:dict] doubleValue]; self.startDelayMinutes = [self objectOrNilForKey:kMORedConfigDataStartDelayMinutes fromDictionary:dict]; NSObject *receivedMORedSendConfigs = [dict objectForKey:kMORedConfigDataRedSendConfigs]; NSMutableArray *parsedMORedSendConfigs = [NSMutableArray array]; if ([receivedMORedSendConfigs isKindOfClass:[NSArray class]]) { for (NSDictionary *item in (NSArray *)receivedMORedSendConfigs) { if ([item isKindOfClass:[NSDictionary class]]) { [parsedMORedSendConfigs addObject:[MORedSendConfigs modelObjectWithDictionary:item]]; } } } else if ([receivedMORedSendConfigs isKindOfClass:[NSDictionary class]]) { [parsedMORedSendConfigs addObject:[MORedSendConfigs modelObjectWithDictionary:(NSDictionary *)receivedMORedSendConfigs]]; } self.redSendConfigs = [NSArray arrayWithArray:parsedMORedSendConfigs]; } return self; } - (NSDictionary *)dictionaryRepresentation { NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary]; [mutableDict setValue:[NSNumber numberWithDouble:self.refundMinutes] forKey:kMORedConfigDataRefundMinutes]; NSMutableArray *tempArrayForStartDelayMinutes = [NSMutableArray array]; for (NSObject *subArrayObject in self.startDelayMinutes) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForStartDelayMinutes addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForStartDelayMinutes addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForStartDelayMinutes] forKey:kMORedConfigDataStartDelayMinutes]; NSMutableArray *tempArrayForRedSendConfigs = [NSMutableArray array]; for (NSObject *subArrayObject in self.redSendConfigs) { if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) { // This class is a model object [tempArrayForRedSendConfigs addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]]; } else { // Generic object [tempArrayForRedSendConfigs addObject:subArrayObject]; } } [mutableDict setValue:[NSArray arrayWithArray:tempArrayForRedSendConfigs] forKey:kMORedConfigDataRedSendConfigs]; 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.refundMinutes = [aDecoder decodeDoubleForKey:kMORedConfigDataRefundMinutes]; self.startDelayMinutes = [aDecoder decodeObjectForKey:kMORedConfigDataStartDelayMinutes]; self.redSendConfigs = [aDecoder decodeObjectForKey:kMORedConfigDataRedSendConfigs]; return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeDouble:_refundMinutes forKey:kMORedConfigDataRefundMinutes]; [aCoder encodeObject:_startDelayMinutes forKey:kMORedConfigDataStartDelayMinutes]; [aCoder encodeObject:_redSendConfigs forKey:kMORedConfigDataRedSendConfigs]; } - (id)copyWithZone:(NSZone *)zone { MORedConfigData *copy = [[MORedConfigData alloc] init]; if (copy) { copy.refundMinutes = self.refundMinutes; copy.startDelayMinutes = [self.startDelayMinutes copyWithZone:zone]; copy.redSendConfigs = [self.redSendConfigs copyWithZone:zone]; } return copy; } @end