YYModelExample.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // YYModelExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/7/18.
  6. // Copyright (c) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYModelExample.h"
  9. #import "YYKit.h"
  10. ////////////////////////////////////////////////////////////////////////////////
  11. #pragma mark Simple Object Example
  12. @interface YYBook : NSObject
  13. @property (nonatomic, copy) NSString *name;
  14. @property (nonatomic, assign) uint64_t pages;
  15. @property (nonatomic, strong) NSDate *publishDate;
  16. @end
  17. @implementation YYBook
  18. @end
  19. static void SimpleObjectExample() {
  20. YYBook *book = [YYBook modelWithJSON:@" \
  21. { \
  22. \"name\": \"Harry Potter\", \
  23. \"pages\": 512, \
  24. \"publishDate\": \"2010-01-01\" \
  25. }"];
  26. NSString *bookJSON = [book modelToJSONString];
  27. NSLog(@"Book: %@", bookJSON);
  28. }
  29. ////////////////////////////////////////////////////////////////////////////////
  30. #pragma mark Nest Object Example
  31. @interface YYUser : NSObject
  32. @property (nonatomic, assign) uint64_t uid;
  33. @property (nonatomic, copy) NSString *name;
  34. @end
  35. @implementation YYUser
  36. @end
  37. @interface YYRepo : NSObject
  38. @property (nonatomic, assign) uint64_t rid;
  39. @property (nonatomic, copy) NSString *name;
  40. @property (nonatomic, strong) NSDate *createTime;
  41. @property (nonatomic, strong) YYUser *owner;
  42. @end
  43. @implementation YYRepo
  44. @end
  45. static void NestObjectExample() {
  46. YYRepo *repo = [YYRepo modelWithJSON:@" \
  47. { \
  48. \"rid\": 123456789, \
  49. \"name\": \"YYKit\", \
  50. \"createTime\" : \"2011-06-09T06:24:26Z\", \
  51. \"owner\": { \
  52. \"uid\" : 989898, \
  53. \"name\" : \"ibireme\" \
  54. } \
  55. }"];
  56. NSString *repoJSON = [repo modelToJSONString];
  57. NSLog(@"Repo: %@", repoJSON);
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////
  60. #pragma mark Container Object Example
  61. @interface YYPhoto : NSObject
  62. @property (nonatomic, copy) NSString *url;
  63. @property (nonatomic, copy) NSString *desc;
  64. @end
  65. @implementation YYPhoto
  66. @end
  67. @interface YYAlbum : NSObject
  68. @property (nonatomic, copy) NSString *name;
  69. @property (nonatomic, strong) NSArray *photos; // Array<YYPhoto>
  70. @property (nonatomic, strong) NSDictionary *likedUsers; // Key:name(NSString) Value:user(YYUser)
  71. @property (nonatomic, strong) NSSet *likedUserIds; // Set<NSNumber>
  72. @end
  73. @implementation YYAlbum
  74. + (NSDictionary *)modelContainerPropertyGenericClass {
  75. return @{@"photos" : YYPhoto.class,
  76. @"likedUsers" : YYUser.class,
  77. @"likedUserIds" : NSNumber.class};
  78. }
  79. @end
  80. static void ContainerObjectExample() {
  81. YYAlbum *album = [YYAlbum modelWithJSON:@" \
  82. { \
  83. \"name\" : \"Happy Birthday\", \
  84. \"photos\" : [ \
  85. { \
  86. \"url\":\"http://example.com/1.png\", \
  87. \"desc\":\"Happy~\" \
  88. }, \
  89. { \
  90. \"url\":\"http://example.com/2.png\", \
  91. \"desc\":\"Yeah!\" \
  92. } \
  93. ], \
  94. \"likedUsers\" : { \
  95. \"Jony\" : {\"uid\":10001,\"name\":\"Jony\"}, \
  96. \"Anna\" : {\"uid\":10002,\"name\":\"Anna\"} \
  97. }, \
  98. \"likedUserIds\" : [10001,10002] \
  99. }"];
  100. NSString *albumJSON = [album modelToJSONString];
  101. NSLog(@"Album: %@", albumJSON);
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. #pragma mark Custom Mapper Example
  105. @interface YYMessage : NSObject
  106. @property (nonatomic, assign) uint64_t messageId;
  107. @property (nonatomic, strong) NSString *content;
  108. @property (nonatomic, strong) NSDate *time;
  109. @end
  110. @implementation YYMessage
  111. + (NSDictionary *)modelCustomPropertyMapper {
  112. return @{@"messageId":@"i",
  113. @"content":@"c",
  114. @"time":@"t"};
  115. }
  116. - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
  117. uint64_t timestamp = [dic unsignedLongLongValueForKey:@"t" default:0];
  118. self.time = [NSDate dateWithTimeIntervalSince1970:timestamp / 1000.0];
  119. return YES;
  120. }
  121. - (void)modelCustomTransformToDictionary:(NSMutableDictionary *)dic {
  122. dic[@"t"] = @([self.time timeIntervalSince1970] * 1000).description;
  123. }
  124. @end
  125. static void CustomMapperExample() {
  126. YYMessage *message = [YYMessage modelWithJSON:@"{\"i\":\"2000000001\",\"c\":\"Hello\",\"t\":\"1437237598000\"}"];
  127. NSString *messageJSON = [message modelToJSONString];
  128. NSLog(@"Book: %@", messageJSON);
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////
  131. #pragma mark Coding/Copying/hash/equal Example
  132. @interface YYShadow :NSObject <NSCoding, NSCopying>
  133. @property (nonatomic, copy) NSString *name;
  134. @property (nonatomic, assign) CGSize size;
  135. @property (nonatomic, strong) UIColor *color;
  136. @end
  137. @implementation YYShadow
  138. - (void)encodeWithCoder:(NSCoder *)aCoder { [self modelEncodeWithCoder:aCoder]; }
  139. - (id)initWithCoder:(NSCoder *)aDecoder { return [self modelInitWithCoder:aDecoder]; }
  140. - (id)copyWithZone:(NSZone *)zone { return [self modelCopy]; }
  141. - (NSUInteger)hash { return [self modelHash]; }
  142. - (BOOL)isEqual:(id)object { return [self modelIsEqual:object]; }
  143. @end
  144. static void CodingCopyingHashEqualExample() {
  145. YYShadow *shadow = [YYShadow new];
  146. shadow.name = @"Test";
  147. shadow.size = CGSizeMake(10, 0);
  148. shadow.color = [UIColor blueColor];
  149. YYShadow *shadow2 = [shadow deepCopy]; // Archive and Unachive
  150. BOOL equal = [shadow isEqual:shadow2];
  151. NSLog(@"shadow equals: %@",equal ? @"YES" : @"NO");
  152. }
  153. @implementation YYModelExample
  154. - (void)runExample {
  155. SimpleObjectExample();
  156. NestObjectExample();
  157. ContainerObjectExample();
  158. CustomMapperExample();
  159. CodingCopyingHashEqualExample();
  160. }
  161. - (void)viewDidLoad {
  162. [super viewDidLoad];
  163. self.view.backgroundColor = [UIColor whiteColor];
  164. UILabel *label = [UILabel new];
  165. label.size = CGSizeMake(kScreenWidth, 30);
  166. label.centerY = self.view.height / 2 - (kiOS7Later ? 0 : 32);
  167. label.textAlignment = NSTextAlignmentCenter;
  168. label.text = @"See code in YYModelExample.m";
  169. [self.view addSubview:label];
  170. [self runExample];
  171. }
  172. @end