RecentCourseModel.m 7.1 KB

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