MOFunctionalList.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MOFunctionalList.m
  3. //
  4. // Created by SuperCabbage on 2025/3/10
  5. // Copyright (c) 2025 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "MOFunctionalList.h"
  8. #import "MOFunctionalI18ns.h"
  9. NSString *const kMOFunctionalListFunctionalI18ns = @"i18ns";
  10. NSString *const kMOFunctionalListIcon = @"icon";
  11. NSString *const kMOFunctionalListName = @"name";
  12. NSString *const kMOFunctionalListJumpLink = @"jumpLink";
  13. @interface MOFunctionalList ()
  14. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  15. @end
  16. @implementation MOFunctionalList
  17. @synthesize functionalI18ns = _functionalI18ns;
  18. @synthesize icon = _icon;
  19. @synthesize name = _name;
  20. @synthesize jumpLink = _jumpLink;
  21. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
  22. return [[self alloc] initWithDictionary:dict];
  23. }
  24. - (instancetype)initWithDictionary:(NSDictionary *)dict {
  25. self = [super init];
  26. // This check serves to make sure that a non-NSDictionary object
  27. // passed into the model class doesn't break the parsing.
  28. if (self && [dict isKindOfClass:[NSDictionary class]]) {
  29. NSObject *receivedMOFunctionalI18ns = [dict objectForKey:kMOFunctionalListFunctionalI18ns];
  30. NSMutableArray *parsedMOFunctionalI18ns = [NSMutableArray array];
  31. if ([receivedMOFunctionalI18ns isKindOfClass:[NSArray class]]) {
  32. for (NSDictionary *item in (NSArray *)receivedMOFunctionalI18ns) {
  33. if ([item isKindOfClass:[NSDictionary class]]) {
  34. [parsedMOFunctionalI18ns addObject:[MOFunctionalI18ns modelObjectWithDictionary:item]];
  35. }
  36. }
  37. } else if ([receivedMOFunctionalI18ns isKindOfClass:[NSDictionary class]]) {
  38. [parsedMOFunctionalI18ns addObject:[MOFunctionalI18ns modelObjectWithDictionary:(NSDictionary *)receivedMOFunctionalI18ns]];
  39. }
  40. self.functionalI18ns = [NSArray arrayWithArray:parsedMOFunctionalI18ns];
  41. self.icon = [self objectOrNilForKey:kMOFunctionalListIcon fromDictionary:dict];
  42. self.name = [self objectOrNilForKey:kMOFunctionalListName fromDictionary:dict];
  43. self.jumpLink = [self objectOrNilForKey:kMOFunctionalListJumpLink fromDictionary:dict];
  44. }
  45. return self;
  46. }
  47. - (NSDictionary *)dictionaryRepresentation {
  48. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  49. NSMutableArray *tempArrayForFunctionalI18ns = [NSMutableArray array];
  50. for (NSObject *subArrayObject in self.functionalI18ns) {
  51. if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
  52. // This class is a model object
  53. [tempArrayForFunctionalI18ns addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
  54. } else {
  55. // Generic object
  56. [tempArrayForFunctionalI18ns addObject:subArrayObject];
  57. }
  58. }
  59. [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFunctionalI18ns] forKey:kMOFunctionalListFunctionalI18ns];
  60. [mutableDict setValue:self.icon forKey:kMOFunctionalListIcon];
  61. [mutableDict setValue:self.name forKey:kMOFunctionalListName];
  62. [mutableDict setValue:self.jumpLink forKey:kMOFunctionalListJumpLink];
  63. return [NSDictionary dictionaryWithDictionary:mutableDict];
  64. }
  65. - (NSString *)description {
  66. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  67. }
  68. #pragma mark - Helper Method
  69. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
  70. id object = [dict objectForKey:aKey];
  71. return [object isEqual:[NSNull null]] ? nil : object;
  72. }
  73. #pragma mark - NSCoding Methods
  74. - (id)initWithCoder:(NSCoder *)aDecoder {
  75. self = [super init];
  76. self.functionalI18ns = [aDecoder decodeObjectForKey:kMOFunctionalListFunctionalI18ns];
  77. self.icon = [aDecoder decodeObjectForKey:kMOFunctionalListIcon];
  78. self.name = [aDecoder decodeObjectForKey:kMOFunctionalListName];
  79. self.jumpLink = [aDecoder decodeObjectForKey:kMOFunctionalListJumpLink];
  80. return self;
  81. }
  82. - (void)encodeWithCoder:(NSCoder *)aCoder
  83. {
  84. [aCoder encodeObject:_functionalI18ns forKey:kMOFunctionalListFunctionalI18ns];
  85. [aCoder encodeObject:_icon forKey:kMOFunctionalListIcon];
  86. [aCoder encodeObject:_name forKey:kMOFunctionalListName];
  87. [aCoder encodeObject:_jumpLink forKey:kMOFunctionalListJumpLink];
  88. }
  89. - (id)copyWithZone:(NSZone *)zone {
  90. MOFunctionalList *copy = [[MOFunctionalList alloc] init];
  91. if (copy) {
  92. copy.functionalI18ns = [self.functionalI18ns copyWithZone:zone];
  93. copy.icon = [self.icon copyWithZone:zone];
  94. copy.name = [self.name copyWithZone:zone];
  95. copy.jumpLink = [self.jumpLink copyWithZone:zone];
  96. copy.isLocal = self.isLocal;
  97. }
  98. return copy;
  99. }
  100. @end