You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 3 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. ![Logo](http://images.cnitblog.com/blog2015/497279/201505/051004316736641.png)
  2. MJExtension
  3. ===
  4. - A fast, convenient and nonintrusive conversion between JSON and model.
  5. - 转换速度快、使用简单方便的字典转模型框架
  6. GitHub:[CoderMJLee](https://github.com/CoderMJLee) | Blog:[mjios(Chinese)](http://www.cnblogs.com/mjios) | PR is welcome,or [feedback](mailto:richermj123go@vip.qq.com)
  7. ## Contents
  8. * [Getting Started 【开始使用】](#Getting_Started)
  9. * [Features 【能做什么】](#Features)
  10. * [Installation 【安装】](#Installation)
  11. * [Examples 【示例】](#Examples)
  12. * [JSON -> Model](#JSON_Model)
  13. * [JSONString -> Model](#JSONString_Model)
  14. * [Model contains model](#Model_contains_model)
  15. * [Model contains model-array](#Model_contains_model_array)
  16. * [Model name - JSON key mapping](#Model_name_JSON_key_mapping)
  17. * [JSON array -> model array](#JSON_array_model_array)
  18. * [Model -> JSON](#Model_JSON)
  19. * [Model array -> JSON array](#Model_array_JSON_array)
  20. * [Core Data](#Core_Data)
  21. * [Coding](#Coding)
  22. * [Camel -> underline](#Camel_underline)
  23. * [NSString -> NSDate, nil -> @""](#NSString_NSDate)
  24. * [More use cases](#More_use_cases)
  25. ---
  26. # <a id="Getting_Started"></a> Getting Started【开始使用】
  27. ## <a id="Features"></a> Features【能做什么】
  28. - MJExtension是一套字典和模型之间互相转换的超轻量级框架
  29. * `JSON` --> `Model`、`Core Data Model`
  30. * `JSONString` --> `Model`、`Core Data Model`
  31. * `Model`、`Core Data Model` --> `JSON`
  32. * `JSON Array` --> `Model Array`、`Core Data Model Array`
  33. * `JSONString` --> `Model Array`、`Core Data Model Array`
  34. * `Model Array`、`Core Data Model Array` --> `JSON Array`
  35. * Coding all properties of model in one line code.
  36. * 只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档)
  37. ## <a id="Installation"></a> Installation【安装】
  38. ### From CocoaPods【使用CocoaPods】
  39. ```ruby
  40. pod 'MJExtension'
  41. ```
  42. ### Manually【手动导入】
  43. - Drag all source files under floder `MJExtension` to your project.【将`MJExtension`文件夹中的所有源代码拽入项目中】
  44. - Import the main header file:`#import "MJExtension.h"`【导入主头文件:`#import "MJExtension.h"`】
  45. ```objc
  46. MJExtension.h
  47. MJConst.h MJConst.m
  48. MJFoundation.h MJFoundation.m
  49. MJProperty.h MJProperty.m
  50. MJType.h MJType.m
  51. NSObject+MJCoding.h NSObject+MJCoding.m
  52. NSObject+MJProperty.h NSObject+MJProperty.m
  53. NSObject+MJKeyValue.h NSObject+MJKeyValue.m
  54. ```
  55. # <a id="Examples"></a> Examples【示例】
  56. ### <a id="JSON_Model"></a> The most simple JSON -> Model【最简单的字典转模型】
  57. ```objc
  58. typedef enum {
  59. SexMale,
  60. SexFemale
  61. } Sex;
  62. @interface User : NSObject
  63. @property (copy, nonatomic) NSString *name;
  64. @property (copy, nonatomic) NSString *icon;
  65. @property (assign, nonatomic) unsigned int age;
  66. @property (copy, nonatomic) NSString *height;
  67. @property (strong, nonatomic) NSNumber *money;
  68. @property (assign, nonatomic) Sex sex;
  69. @property (assign, nonatomic, getter=isGay) BOOL gay;
  70. @end
  71. /***********************************************/
  72. NSDictionary *dict = @{
  73. @"name" : @"Jack",
  74. @"icon" : @"lufy.png",
  75. @"age" : @20,
  76. @"height" : @"1.55",
  77. @"money" : @100.9,
  78. @"sex" : @(SexFemale),
  79. @"gay" : @"true"
  80. // @"gay" : @"1"
  81. // @"gay" : @"NO"
  82. };
  83. // JSON -> User
  84. User *user = [User mj_objectWithKeyValues:dict];
  85. NSLog(@"name=%@, icon=%@, age=%zd, height=%@, money=%@, sex=%d, gay=%d", user.name, user.icon, user.age, user.height, user.money, user.sex, user.gay);
  86. // name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1
  87. ```
  88. ### <a id="JSONString_Model"></a> JSONString -> Model【JSON字符串转模型】
  89. ```objc
  90. // 1.Define a JSONString
  91. NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
  92. // 2.JSONString -> User
  93. User *user = [User mj_objectWithKeyValues:jsonString];
  94. // 3.Print user's properties
  95. NSLog(@"name=%@, icon=%@, age=%d", user.name, user.icon, user.age);
  96. // name=Jack, icon=lufy.png, age=20
  97. ```
  98. ### <a id="Model_contains_model"></a> Model contains model【模型中嵌套模型】
  99. ```objc
  100. @interface Status : NSObject
  101. @property (copy, nonatomic) NSString *text;
  102. @property (strong, nonatomic) User *user;
  103. @property (strong, nonatomic) Status *retweetedStatus;
  104. @end
  105. /***********************************************/
  106. NSDictionary *dict = @{
  107. @"text" : @"Agree!Nice weather!",
  108. @"user" : @{
  109. @"name" : @"Jack",
  110. @"icon" : @"lufy.png"
  111. },
  112. @"retweetedStatus" : @{
  113. @"text" : @"Nice weather!",
  114. @"user" : @{
  115. @"name" : @"Rose",
  116. @"icon" : @"nami.png"
  117. }
  118. }
  119. };
  120. // JSON -> Status
  121. Status *status = [Status mj_objectWithKeyValues:dict];
  122. NSString *text = status.text;
  123. NSString *name = status.user.name;
  124. NSString *icon = status.user.icon;
  125. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  126. // text=Agree!Nice weather!, name=Jack, icon=lufy.png
  127. NSString *text2 = status.retweetedStatus.text;
  128. NSString *name2 = status.retweetedStatus.user.name;
  129. NSString *icon2 = status.retweetedStatus.user.icon;
  130. NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
  131. // text2=Nice weather!, name2=Rose, icon2=nami.png
  132. ```
  133. ### <a id="Model_contains_model_array"></a> Model contains model-array【模型中有个数组属性,数组里面又要装着其他模型】
  134. ```objc
  135. @interface Ad : NSObject
  136. @property (copy, nonatomic) NSString *image;
  137. @property (copy, nonatomic) NSString *url;
  138. @end
  139. @interface StatusResult : NSObject
  140. /** Contatins status model */
  141. @property (strong, nonatomic) NSMutableArray *statuses;
  142. /** Contatins ad model */
  143. @property (strong, nonatomic) NSArray *ads;
  144. @property (strong, nonatomic) NSNumber *totalNumber;
  145. @end
  146. /***********************************************/
  147. // Tell MJExtension what type model will be contained in statuses and ads.
  148. [StatusResult mj_setupObjectClassInArray:^NSDictionary *{
  149. return @{
  150. @"statuses" : @"Status",
  151. // @"statuses" : [Status class],
  152. @"ads" : @"Ad"
  153. // @"ads" : [Ad class]
  154. };
  155. }];
  156. // Equals: StatusResult.m implements +mj_objectClassInArray method.
  157. NSDictionary *dict = @{
  158. @"statuses" : @[
  159. @{
  160. @"text" : @"Nice weather!",
  161. @"user" : @{
  162. @"name" : @"Rose",
  163. @"icon" : @"nami.png"
  164. }
  165. },
  166. @{
  167. @"text" : @"Go camping tomorrow!",
  168. @"user" : @{
  169. @"name" : @"Jack",
  170. @"icon" : @"lufy.png"
  171. }
  172. }
  173. ],
  174. @"ads" : @[
  175. @{
  176. @"image" : @"ad01.png",
  177. @"url" : @"http://www.ad01.com"
  178. },
  179. @{
  180. @"image" : @"ad02.png",
  181. @"url" : @"http://www.ad02.com"
  182. }
  183. ],
  184. @"totalNumber" : @"2014"
  185. };
  186. // JSON -> StatusResult
  187. StatusResult *result = [StatusResult mj_objectWithKeyValues:dict];
  188. NSLog(@"totalNumber=%@", result.totalNumber);
  189. // totalNumber=2014
  190. // Printing
  191. for (Status *status in result.statuses) {
  192. NSString *text = status.text;
  193. NSString *name = status.user.name;
  194. NSString *icon = status.user.icon;
  195. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  196. }
  197. // text=Nice weather!, name=Rose, icon=nami.png
  198. // text=Go camping tomorrow!, name=Jack, icon=lufy.png
  199. // Printing
  200. for (Ad *ad in result.ads) {
  201. NSLog(@"image=%@, url=%@", ad.image, ad.url);
  202. }
  203. // image=ad01.png, url=http://www.ad01.com
  204. // image=ad02.png, url=http://www.ad02.com
  205. ```
  206. ### <a id="Model_name_JSON_key_mapping"></a> Model name - JSON key mapping【模型中的属性名和字典中的key不相同(或者需要多级映射)】
  207. ```objc
  208. @interface Bag : NSObject
  209. @property (copy, nonatomic) NSString *name;
  210. @property (assign, nonatomic) double price;
  211. @end
  212. @interface Student : NSObject
  213. @property (copy, nonatomic) NSString *ID;
  214. @property (copy, nonatomic) NSString *desc;
  215. @property (copy, nonatomic) NSString *nowName;
  216. @property (copy, nonatomic) NSString *oldName;
  217. @property (copy, nonatomic) NSString *nameChangedTime;
  218. @property (strong, nonatomic) Bag *bag;
  219. @end
  220. /***********************************************/
  221. // How to map
  222. [Student mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
  223. return @{
  224. @"ID" : @"id",
  225. @"desc" : @"desciption",
  226. @"oldName" : @"name.oldName",
  227. @"nowName" : @"name.newName",
  228. @"nameChangedTime" : @"name.info[1].nameChangedTime",
  229. @"bag" : @"other.bag"
  230. };
  231. }];
  232. // Equals: Student.m implements +mj_replacedKeyFromPropertyName method.
  233. NSDictionary *dict = @{
  234. @"id" : @"20",
  235. @"desciption" : @"kids",
  236. @"name" : @{
  237. @"newName" : @"lufy",
  238. @"oldName" : @"kitty",
  239. @"info" : @[
  240. @"test-data",
  241. @{
  242. @"nameChangedTime" : @"2013-08"
  243. }
  244. ]
  245. },
  246. @"other" : @{
  247. @"bag" : @{
  248. @"name" : @"a red bag",
  249. @"price" : @100.7
  250. }
  251. }
  252. };
  253. // JSON -> Student
  254. Student *stu = [Student mj_objectWithKeyValues:dict];
  255. // Printing
  256. NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
  257. stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
  258. // ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
  259. NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
  260. // bagName=a red bag, bagPrice=100.700000
  261. ```
  262. ### <a id="JSON_array_model_array"></a> JSON array -> model array【将一个字典数组转成模型数组】
  263. ```objc
  264. NSArray *dictArray = @[
  265. @{
  266. @"name" : @"Jack",
  267. @"icon" : @"lufy.png"
  268. },
  269. @{
  270. @"name" : @"Rose",
  271. @"icon" : @"nami.png"
  272. }
  273. ];
  274. // JSON array -> User array
  275. NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
  276. // Printing
  277. for (User *user in userArray) {
  278. NSLog(@"name=%@, icon=%@", user.name, user.icon);
  279. }
  280. // name=Jack, icon=lufy.png
  281. // name=Rose, icon=nami.png
  282. ```
  283. ### <a id="Model_JSON"></a> Model -> JSON【将一个模型转成字典】
  284. ```objc
  285. // New model
  286. User *user = [[User alloc] init];
  287. user.name = @"Jack";
  288. user.icon = @"lufy.png";
  289. Status *status = [[Status alloc] init];
  290. status.user = user;
  291. status.text = @"Nice mood!";
  292. // Status -> JSON
  293. NSDictionary *statusDict = status.mj_keyValues;
  294. NSLog(@"%@", statusDict);
  295. /*
  296. {
  297. text = "Nice mood!";
  298. user = {
  299. icon = "lufy.png";
  300. name = Jack;
  301. };
  302. }
  303. */
  304. // More complex situation
  305. Student *stu = [[Student alloc] init];
  306. stu.ID = @"123";
  307. stu.oldName = @"rose";
  308. stu.nowName = @"jack";
  309. stu.desc = @"handsome";
  310. stu.nameChangedTime = @"2018-09-08";
  311. Bag *bag = [[Bag alloc] init];
  312. bag.name = @"a red bag";
  313. bag.price = 205;
  314. stu.bag = bag;
  315. NSDictionary *stuDict = stu.mj_keyValues;
  316. NSLog(@"%@", stuDict);
  317. /*
  318. {
  319. ID = 123;
  320. bag = {
  321. name = "\U5c0f\U4e66\U5305";
  322. price = 205;
  323. };
  324. desc = handsome;
  325. nameChangedTime = "2018-09-08";
  326. nowName = jack;
  327. oldName = rose;
  328. }
  329. */
  330. ```
  331. ### <a id="Model_array_JSON_array"></a> Model array -> JSON array【将一个模型数组转成字典数组】
  332. ```objc
  333. // New model array
  334. User *user1 = [[User alloc] init];
  335. user1.name = @"Jack";
  336. user1.icon = @"lufy.png";
  337. User *user2 = [[User alloc] init];
  338. user2.name = @"Rose";
  339. user2.icon = @"nami.png";
  340. NSArray *userArray = @[user1, user2];
  341. // Model array -> JSON array
  342. NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
  343. NSLog(@"%@", dictArray);
  344. /*
  345. (
  346. {
  347. icon = "lufy.png";
  348. name = Jack;
  349. },
  350. {
  351. icon = "nami.png";
  352. name = Rose;
  353. }
  354. )
  355. */
  356. ```
  357. ### <a id="Core_Data"></a> Core Data
  358. ```objc
  359. NSDictionary *dict = @{
  360. @"name" : @"Jack",
  361. @"icon" : @"lufy.png",
  362. @"age" : @20,
  363. @"height" : @1.55,
  364. @"money" : @"100.9",
  365. @"sex" : @(SexFemale),
  366. @"gay" : @"true"
  367. };
  368. // This demo just provide simple steps
  369. NSManagedObjectContext *context = nil;
  370. User *user = [User mj_objectWithKeyValues:dict context:context];
  371. [context save:nil];
  372. ```
  373. ### <a id="Coding"></a> Coding
  374. ```objc
  375. #import "MJExtension.h"
  376. @implementation Bag
  377. // NSCoding Implementation
  378. MJExtensionCodingImplementation
  379. @end
  380. /***********************************************/
  381. // what properties not to be coded
  382. [Bag mj_setupIgnoredCodingPropertyNames:^NSArray *{
  383. return @[@"name"];
  384. }];
  385. // Equals: Bag.m implements +mj_ignoredCodingPropertyNames method.
  386. // Create model
  387. Bag *bag = [[Bag alloc] init];
  388. bag.name = @"Red bag";
  389. bag.price = 200.8;
  390. NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"];
  391. // Encoding
  392. [NSKeyedArchiver archiveRootObject:bag toFile:file];
  393. // Decoding
  394. Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
  395. NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
  396. // name=(null), price=200.800000
  397. ```
  398. ### <a id="Camel_underline"></a> Camel -> underline【统一转换属性名(比如驼峰转下划线)】
  399. ```objc
  400. // Dog
  401. #import "MJExtension.h"
  402. @implementation Dog
  403. + (NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName
  404. {
  405. // nickName -> nick_name
  406. return [propertyName mj_underlineFromCamel];
  407. }
  408. @end
  409. // NSDictionary
  410. NSDictionary *dict = @{
  411. @"nick_name" : @"旺财",
  412. @"sale_price" : @"10.5",
  413. @"run_speed" : @"100.9"
  414. };
  415. // NSDictionary -> Dog
  416. Dog *dog = [Dog mj_objectWithKeyValues:dict];
  417. // printing
  418. NSLog(@"nickName=%@, scalePrice=%f runSpeed=%f", dog.nickName, dog.salePrice, dog.runSpeed);
  419. ```
  420. ### <a id="NSString_NSDate"></a> NSString -> NSDate, nil -> @""【过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@"")】
  421. ```objc
  422. // Book
  423. #import "MJExtension.h"
  424. @implementation Book
  425. - (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
  426. {
  427. if ([property.name isEqualToString:@"publisher"]) {
  428. if (oldValue == nil) return @"";
  429. } else if (property.type.typeClass == [NSDate class]) {
  430. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  431. fmt.dateFormat = @"yyyy-MM-dd";
  432. return [fmt dateFromString:oldValue];
  433. }
  434. return oldValue;
  435. }
  436. @end
  437. // NSDictionary
  438. NSDictionary *dict = @{
  439. @"name" : @"5分钟突破iOS开发",
  440. @"publishedTime" : @"2011-09-10"
  441. };
  442. // NSDictionary -> Book
  443. Book *book = [Book mj_objectWithKeyValues:dict];
  444. // printing
  445. NSLog(@"name=%@, publisher=%@, publishedTime=%@", book.name, book.publisher, book.publishedTime);
  446. ```
  447. ### <a id="More_use_cases"></a> More use cases【更多用法】
  448. - Please reference to `NSObject+MJKeyValue.h` and `NSObject+MJCoding.h`
  449. ## 期待
  450. * 如果在使用过程中遇到BUG,希望你能Issues我,谢谢(或者尝试下载最新的框架代码看看BUG修复没有)
  451. * 如果在使用过程中发现功能不够用,希望你能Issues我,我非常想为这个框架增加更多好用的功能,谢谢
  452. * 如果你想为MJExtension输出代码,请拼命Pull Requests我