MOHelpList.m 2.3 KB

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