MOFunctionData.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // MOFunctionData.m
  3. //
  4. // Created by SuperCabbage on 2025/3/10
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOFunctionData.h"
  8. #import "MOFunctionalList.h"
  9. NSString *const kMOFunctionDataFunctionalList = @"list";
  10. @interface MOFunctionData ()
  11. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  12. @end
  13. @implementation MOFunctionData
  14. @synthesize functionalList = _functionalList;
  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 *receivedMOFunctionalList = [dict objectForKey:kMOFunctionDataFunctionalList];
  24. NSMutableArray *parsedMOFunctionalList = [NSMutableArray array];
  25. if ([receivedMOFunctionalList isKindOfClass:[NSArray class]]) {
  26. for (NSDictionary *item in (NSArray *)receivedMOFunctionalList) {
  27. if ([item isKindOfClass:[NSDictionary class]]) {
  28. [parsedMOFunctionalList addObject:[MOFunctionalList modelObjectWithDictionary:item]];
  29. }
  30. }
  31. } else if ([receivedMOFunctionalList isKindOfClass:[NSDictionary class]]) {
  32. [parsedMOFunctionalList addObject:[MOFunctionalList modelObjectWithDictionary:(NSDictionary *)receivedMOFunctionalList]];
  33. }
  34. self.functionalList = [NSArray arrayWithArray:parsedMOFunctionalList];
  35. }
  36. return self;
  37. }
  38. - (NSDictionary *)dictionaryRepresentation {
  39. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  40. NSMutableArray *tempArrayForFunctionalList = [NSMutableArray array];
  41. for (NSObject *subArrayObject in self.functionalList) {
  42. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  43. // This class is a model object
  44. [tempArrayForFunctionalList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  45. } else {
  46. // Generic object
  47. [tempArrayForFunctionalList addObject:subArrayObject];
  48. }
  49. }
  50. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFunctionalList] forKey:kMOFunctionDataFunctionalList];
  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.functionalList = [aDecoder decodeObjectForKey:kMOFunctionDataFunctionalList];
  65. return self;
  66. }
  67. - (void)encodeWithCoder:(NSCoder *)aCoder
  68. {
  69. [aCoder encodeObject:_functionalList forKey:kMOFunctionDataFunctionalList];
  70. }
  71. - (id)copyWithZone:(NSZone *)zone {
  72. MOFunctionData *copy = [[MOFunctionData alloc] init];
  73. if (copy) {
  74. copy.functionalList = [self.functionalList copyWithZone:zone];
  75. }
  76. return copy;
  77. }
  78. @end