| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // MOWameBaseData.m
- //
- // Created by SuperCabbage on 2024/4/29
- // Copyright (c) 2024 __MyCompanyName__. All rights reserved.
- //
- #import "MOWameBaseData.h"
- #import "MOWamelist.h"
- NSString *const kMOWameBaseDataWamelist = @"list";
- @interface MOWameBaseData ()
- - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
- @end
- @implementation MOWameBaseData
- @synthesize wamelist = _wamelist;
- + (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 *receivedMOWamelist = [dict objectForKey:kMOWameBaseDataWamelist];
- NSMutableArray *parsedMOWamelist = [NSMutableArray array];
-
- if ([receivedMOWamelist isKindOfClass:[NSArray class]]) {
- for (NSDictionary *item in (NSArray *)receivedMOWamelist) {
- if ([item isKindOfClass:[NSDictionary class]]) {
- [parsedMOWamelist addObject:[MOWamelist modelObjectWithDictionary:item]];
- }
- }
- } else if ([receivedMOWamelist isKindOfClass:[NSDictionary class]]) {
- [parsedMOWamelist addObject:[MOWamelist modelObjectWithDictionary:(NSDictionary *)receivedMOWamelist]];
- }
- self.wamelist = [NSArray arrayWithArray:parsedMOWamelist];
- }
-
- return self;
-
- }
- - (NSDictionary *)dictionaryRepresentation {
- NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
- NSMutableArray *tempArrayForWamelist = [NSMutableArray array];
-
- for (NSObject *subArrayObject in self.wamelist) {
- if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
- // This class is a model object
- [tempArrayForWamelist addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
- } else {
- // Generic object
- [tempArrayForWamelist addObject:subArrayObject];
- }
- }
- [mutableDict setValue:[NSArray arrayWithArray:tempArrayForWamelist] forKey:kMOWameBaseDataWamelist];
- 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.wamelist = [aDecoder decodeObjectForKey:kMOWameBaseDataWamelist];
- return self;
- }
- - (void)encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:_wamelist forKey:kMOWameBaseDataWamelist];
- }
- - (id)copyWithZone:(NSZone *)zone {
- MOWameBaseData *copy = [[MOWameBaseData alloc] init];
-
-
-
- if (copy) {
- copy.wamelist = [self.wamelist copyWithZone:zone];
- }
-
- return copy;
- }
- @end
|