MOConfigList.m 2.7 KB

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