MOUserListData.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // MOUserListData.m
  3. //
  4. // Created by SuperCabbage on 2023/11/28
  5. // Copyright (c) 2023 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOUserListData.h"
  8. #import "MOUserBase.h"
  9. NSString *const kMOUserListDataUserBase = @"list";
  10. @interface MOUserListData ()
  11. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  12. @end
  13. @implementation MOUserListData
  14. @synthesize userBase = _userBase;
  15. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  16. return [[self alloc] initWithDictionary:dict];
  17. }
  18. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  19. self = [super init];
  20. // This check serves to make sure that a non-NSDictionary object
  21. // passed into the model class doesn't break the parsing.
  22. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  23. NSObject *receivedMOUserBase = [dict objectForKey:kMOUserListDataUserBase];
  24. NSMutableArray *parsedMOUserBase = [NSMutableArray array];
  25. if ([receivedMOUserBase isKindOfClass:[NSArray class]]) {
  26. for (NSDictionary *item in (NSArray *)receivedMOUserBase) {
  27. if ([item isKindOfClass:[NSDictionary class]]) {
  28. [parsedMOUserBase addObject:[MOUserBase modelObjectWithDictionary:item]];
  29. }
  30. }
  31. } else if ([receivedMOUserBase isKindOfClass:[NSDictionary class]]) {
  32. [parsedMOUserBase addObject:[MOUserBase modelObjectWithDictionary:(NSDictionary *)receivedMOUserBase]];
  33. }
  34. self.userBase = [NSArray arrayWithArray:parsedMOUserBase];
  35. }
  36. return self;
  37. }
  38. - (NSDictionary *)dictionaryRepresentation {
  39. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  40. NSMutableArray *tempArrayForUserBase = [NSMutableArray array];
  41. for (NSObject *subArrayObject in self.userBase) {
  42. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  43. // This class is a model object
  44. [tempArrayForUserBase addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  45. } else {
  46. // Generic object
  47. [tempArrayForUserBase addObject:subArrayObject];
  48. }
  49. }
  50. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForUserBase] forKey:kMOUserListDataUserBase];
  51. return [NSDictionary dictionaryWithDictionary:mutableDict];
  52. }
  53. - (NSString *)description {
  54. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  55. }
  56. #pragma mark - Helper Method
  57. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  58. id object = [dict objectForKey:aKey];
  59. return [object isEqual:[NSNull null]] ? nil : object;
  60. }
  61. #pragma mark - NSCoding Methods
  62. - (id)initWithCoder:(NSCoder *)aDecoder {
  63. self = [super init];
  64. self.userBase = [aDecoder decodeObjectForKey:kMOUserListDataUserBase];
  65. return self;
  66. }
  67. - (void)encodeWithCoder:(NSCoder *)aCoder
  68. {
  69. [aCoder encodeObject:_userBase forKey:kMOUserListDataUserBase];
  70. }
  71. - (id)copyWithZone:(NSZone *)zone {
  72. MOUserListData *copy = [[MOUserListData alloc] init];
  73. if (copy) {
  74. copy.userBase = [self.userBase copyWithZone:zone];
  75. }
  76. return copy;
  77. }
  78. @end