RecentCourseModel.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // RecentCourseModel.m
  3. //
  4. // Created by Steven on 2022/5/10
  5. // Copyright (c) 2022 __MyCompanyName__. All rights reserved.
  6. //
  7. #import "RecentCourseModel.h"
  8. NSString *const kRecentCourseModelStatus = @"status";
  9. NSString *const kRecentCourseModelCourseGroupId = @"courseGroupId";
  10. NSString *const kRecentCourseModelRealName = @"realName";
  11. NSString *const kRecentCourseModelAvatar = @"avatar";
  12. NSString *const kRecentCourseModelCourseId = @"courseId";
  13. NSString *const kRecentCourseModelCourseType = @"courseType";
  14. NSString *const kRecentCourseModelTeacherName = @"teacherName";
  15. NSString *const kRecentCourseModelCourseStartTime = @"courseStartTime";
  16. NSString *const kRecentCourseModelTeacherId = @"teacherId";
  17. NSString *const kRecentCourseModelCourseGroupName = @"courseGroupName";
  18. @interface RecentCourseModel ()
  19. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;
  20. @end
  21. @implementation RecentCourseModel
  22. @synthesize status = _status;
  23. @synthesize courseGroupId = _courseGroupId;
  24. @synthesize realName = _realName;
  25. @synthesize avatar = _avatar;
  26. @synthesize courseId = _courseId;
  27. @synthesize courseType = _courseType;
  28. @synthesize teacherName = _teacherName;
  29. @synthesize courseStartTime = _courseStartTime;
  30. @synthesize teacherId = _teacherId;
  31. @synthesize courseGroupName = _courseGroupName;
  32. + (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
  33. {
  34. return [[self alloc] initWithDictionary:dict];
  35. }
  36. - (instancetype)initWithDictionary:(NSDictionary *)dict
  37. {
  38. self = [super init];
  39. // This check serves to make sure that a non-NSDictionary object
  40. // passed into the model class doesn't break the parsing.
  41. if(self && [dict isKindOfClass:[NSDictionary class]]) {
  42. self.status = [self objectOrNilForKey:kRecentCourseModelStatus fromDictionary:dict];
  43. self.courseGroupId = [self objectOrNilForKey:kRecentCourseModelCourseGroupId fromDictionary:dict];
  44. self.realName = [self objectOrNilForKey:kRecentCourseModelRealName fromDictionary:dict];
  45. self.avatar = [self objectOrNilForKey:kRecentCourseModelAvatar fromDictionary:dict];
  46. self.courseId = [self objectOrNilForKey:kRecentCourseModelCourseId fromDictionary:dict];
  47. self.courseType = [self objectOrNilForKey:kRecentCourseModelCourseType fromDictionary:dict];
  48. self.teacherName = [self objectOrNilForKey:kRecentCourseModelTeacherName fromDictionary:dict];
  49. self.courseStartTime = [self objectOrNilForKey:kRecentCourseModelCourseStartTime fromDictionary:dict];
  50. self.teacherId = [self objectOrNilForKey:kRecentCourseModelTeacherId fromDictionary:dict];
  51. self.courseGroupName = [self objectOrNilForKey:kRecentCourseModelCourseGroupName fromDictionary:dict];
  52. }
  53. return self;
  54. }
  55. - (NSDictionary *)dictionaryRepresentation
  56. {
  57. NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
  58. [mutableDict setValue:self.status forKey:kRecentCourseModelStatus];
  59. [mutableDict setValue:self.courseGroupId forKey:kRecentCourseModelCourseGroupId];
  60. [mutableDict setValue:self.realName forKey:kRecentCourseModelRealName];
  61. [mutableDict setValue:self.avatar forKey:kRecentCourseModelAvatar];
  62. [mutableDict setValue:self.courseId forKey:kRecentCourseModelCourseId];
  63. [mutableDict setValue:self.courseType forKey:kRecentCourseModelCourseType];
  64. [mutableDict setValue:self.teacherName forKey:kRecentCourseModelTeacherName];
  65. [mutableDict setValue:self.courseStartTime forKey:kRecentCourseModelCourseStartTime];
  66. [mutableDict setValue:self.teacherId forKey:kRecentCourseModelTeacherId];
  67. [mutableDict setValue:self.courseGroupName forKey:kRecentCourseModelCourseGroupName];
  68. return [NSDictionary dictionaryWithDictionary:mutableDict];
  69. }
  70. - (NSString *)description
  71. {
  72. return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
  73. }
  74. #pragma mark - Helper Method
  75. - (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
  76. {
  77. id object = [dict objectForKey:aKey];
  78. return [object isEqual:[NSNull null]] ? nil : object;
  79. }
  80. #pragma mark - NSCoding Methods
  81. - (id)initWithCoder:(NSCoder *)aDecoder
  82. {
  83. self = [super init];
  84. self.status = [aDecoder decodeObjectForKey:kRecentCourseModelStatus];
  85. self.courseGroupId = [aDecoder decodeObjectForKey:kRecentCourseModelCourseGroupId];
  86. self.realName = [aDecoder decodeObjectForKey:kRecentCourseModelRealName];
  87. self.avatar = [aDecoder decodeObjectForKey:kRecentCourseModelAvatar];
  88. self.courseId = [aDecoder decodeObjectForKey:kRecentCourseModelCourseId];
  89. self.courseType = [aDecoder decodeObjectForKey:kRecentCourseModelCourseType];
  90. self.teacherName = [aDecoder decodeObjectForKey:kRecentCourseModelTeacherName];
  91. self.courseStartTime = [aDecoder decodeObjectForKey:kRecentCourseModelCourseStartTime];
  92. self.teacherId = [aDecoder decodeObjectForKey:kRecentCourseModelTeacherId];
  93. self.courseGroupName = [aDecoder decodeObjectForKey:kRecentCourseModelCourseGroupName];
  94. return self;
  95. }
  96. - (void)encodeWithCoder:(NSCoder *)aCoder
  97. {
  98. [aCoder encodeObject:_status forKey:kRecentCourseModelStatus];
  99. [aCoder encodeObject:_courseGroupId forKey:kRecentCourseModelCourseGroupId];
  100. [aCoder encodeObject:_realName forKey:kRecentCourseModelRealName];
  101. [aCoder encodeObject:_avatar forKey:kRecentCourseModelAvatar];
  102. [aCoder encodeObject:_courseId forKey:kRecentCourseModelCourseId];
  103. [aCoder encodeObject:_courseType forKey:kRecentCourseModelCourseType];
  104. [aCoder encodeObject:_teacherName forKey:kRecentCourseModelTeacherName];
  105. [aCoder encodeObject:_courseStartTime forKey:kRecentCourseModelCourseStartTime];
  106. [aCoder encodeObject:_teacherId forKey:kRecentCourseModelTeacherId];
  107. [aCoder encodeObject:_courseGroupName forKey:kRecentCourseModelCourseGroupName];
  108. }
  109. - (id)copyWithZone:(NSZone *)zone
  110. {
  111. RecentCourseModel *copy = [[RecentCourseModel alloc] init];
  112. if (copy) {
  113. copy.status = [self.status copyWithZone:zone];
  114. copy.courseGroupId = [self.courseGroupId copyWithZone:zone];
  115. copy.realName = [self.realName copyWithZone:zone];
  116. copy.avatar = [self.avatar copyWithZone:zone];
  117. copy.courseId = [self.courseId copyWithZone:zone];
  118. copy.courseType = [self.courseType copyWithZone:zone];
  119. copy.teacherName = [self.teacherName copyWithZone:zone];
  120. copy.courseStartTime = [self.courseStartTime copyWithZone:zone];
  121. copy.teacherId = [self.teacherId copyWithZone:zone];
  122. copy.courseGroupName = [self.courseGroupName copyWithZone:zone];
  123. }
  124. return copy;
  125. }
  126. @end