当前位置:   article > 正文

MJExtension的使用方法_mjextension点语法

mjextension点语法

1.简单的字典 --> 模型

  • 核心代码 mj_objectWithKeyValues:
    1. typedef enum {
    2. SexMale,
    3. SexFemale
    4. } Sex;
    5. @interface User : NSObject
    6. @property (copy, nonatomic) NSString *name;/* 姓名 */
    7. @property (copy, nonatomic) NSString *icon;/* 头像 */
    8. @property (assign, nonatomic) unsigned int age;/* 年龄 */
    9. @property (copy, nonatomic) NSString *height;/* 身高 */
    10. @property (strong, nonatomic) NSNumber *money;/* 资产 */
    11. @property (assign, nonatomic) Sex sex;/* 性别 */
    12. @property (assign, nonatomic, getter=isGay) BOOL gay;/* 是否是同性恋 */
    13. @end
    14. //简单的字典
    15. NSDictionary *dict_user = @{
    16. @"name" : @"Jack",
    17. @"icon" : @"lufy.png",
    18. @"age" : @20,
    19. @"height" : @"1.55",
    20. @"money" : @100.9,
    21. @"sex" : @(SexFemale),/* 枚举需要使用NSNumber包装 */
    22. @"gay" : @YES
    23. };
    24. User *user = [User mj_objectWithKeyValues:dict_user];
    25. NSLog(@"MJ---%@----%@---%u---%@---%@---%u----%d",user.name,user.icon,user.age,user.height,user.money,user.sex,user.gay);
    26. //打印结果
    27. //2016-07-04 11:06:59.746 PPDemos[2432:73824] MJ---Jack----lufy.png---20---1.55---100.9---1----1

2. JSON字符串 --> 模型

  • 核心代码 mj_objectWithKeyValues:
    1. // 定义一个JSON字符串
    2. NSString *jsonStr = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
    3. User *user = [User mj_objectWithKeyValues:jsonStr];
    4. NSLog(@"MJ---%@----%@---%u",user.name,user.icon,user.age);
    5. //打印结果
    6. //2016-07-04 11:16:04.655 PPDemos[2563:78561] MJ---Jack----lufy.png---20

3. 复杂的字典 --> 模型 (模型里面包含了模型)

  • 核心代码 mj_objectWithKeyValues:
    1. //复杂的字典[模型中有个数组属性,数组里面又要装着其他模型的字典]
    2. NSDictionary *dict_m8m = @{
    3. @"text" : @"Agree!Nice weather!",
    4. @"user" : @{
    5. @"name" : @"Jack",
    6. @"icon" : @"lufy.png"
    7. },
    8. @"retweetedStatus" : @{
    9. @"text" : @"Nice weather!",
    10. @"user" : @{
    11. @"name" : @"Rose",
    12. @"icon" : @"nami.png"
    13. }
    14. }
    15. };
    1. #import <Foundation/Foundation.h>
    2. @class User;
    3. @class Status;
    4. //Status模型
    5. @interface Status : NSObject
    6. @property (copy, nonatomic) NSString *text;
    7. @property (strong, nonatomic) User *user;/* 其他模型类型 */
    8. @property (strong, nonatomic) Status *retweetedStatus;/* 自我模型类型 */
    9. @end
    10. //
    11. //字典转模型,模型里面含有模型
    12. Status *status = [Status mj_objectWithKeyValues:dict_m8m];
    13. NSString *text = status.text;
    14. NSString *name = status.user.name;
    15. NSString *icon = status.user.icon;
    16. NSLog(@"mj-----text=%@, name=%@, icon=%@", text, name, icon);
    17. NSString *text2 = status.retweetedStatus.text;
    18. NSString *name2 = status.retweetedStatus.user.name;
    19. NSString *icon2 = status.retweetedStatus.user.icon;
    20. NSLog(@"mj-----text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
    21. //
    22. //打印结果
    23. //2016-07-04 11:45:39.675 PPDemos[2781:87089] mj-----text=Agree!Nice weather!, name=Jack, icon=lufy.png
    24. //2016-07-04 11:45:39.675 PPDemos[2781:87089] mj-----text2=Nice weather!, name2=Rose, icon2=nami.png

4.模型中有个数组属性,数组里面又要装着其它模型

  • 核心代码 mj_objectWithKeyValues:mj_objectClassInArray
  1. @interface Ad : NSObject
  2. @property (copy, nonatomic) NSString *image;
  3. @property (copy, nonatomic) NSString *url;
  4. @end
  1. @interface StatusResult : NSObject
  2. /** 存放着一堆的微博数据(里面都是Status模型) */
  3. @property (strong, nonatomic) NSMutableArray *statuses;
  4. /** 存放着一堆的广告数据(里面都是Ad模型) */
  5. @property (strong, nonatomic) NSArray *ads;
  6. @property (strong, nonatomic) NSNumber *totalNumber;
  7. @end
  1. #import "StatusResult.h"
  2. #import "MJExtension.h"
  3. @implementation StatusResult
  4. /* 数组中存储模型数据,需要说明数组中存储的模型数据类型 */
  5. +(NSDictionary *)mj_objectClassInArray
  6. {
  7. return @{
  8. @"statuses" : @"Status",
  9. @"ads" : @"Ad"
  10. };
  11. }
  12. @end
在VC里实现以下来解析数据
  1. //模型中有个数组属性,数组里面又要装着其他模型
  2. NSDictionry *dict_m8a = @{
  3. @"statuses" : @[
  4. @{
  5. @"text" : @"Nice weather!",
  6. @"user" : @{
  7. @"name" : @"Rose",
  8. @"icon" : @"nami.png"
  9. }
  10. },
  11. @{
  12. @"text" : @"Go camping tomorrow!",
  13. @"user" : @{
  14. @"name" : @"Jack",
  15. @"icon" : @"lufy.png"
  16. }
  17. }
  18. ],
  19. @"ads" : @[
  20. @{
  21. @"image" : @"ad01.png",
  22. @"url" : @"http://www.ad01.com"
  23. },
  24. @{
  25. @"image" : @"ad02.png",
  26. @"url" : @"http://www.ad02.com"
  27. }
  28. ],
  29. @"totalNumber" : @"2014"
  30. };
  31. 【重点,核心】》》数组中存储模型数据,需要说明数组中存储的模型数据类型 《《
  32. /*
  33. [StatusResult mj_setupObjectClassInArray:^NSDictionary *{
  34. return @{
  35. @"statuses" : @"Status",
  36. // @"statuses" : [Status class],
  37. @"ads" : @"Ad"
  38. // @"ads" : [Ad class]
  39. };
  40. }];
  41. // Equals: StatusResult.m implements +mj_objectClassInArray method.
  42. */
  43. //以上方法在VC里写,如果多个地方解析该model,就要写多次,最好在model的.m文件写!
  44. //字典转模型,支持模型的数组属性里面又装着模型
  45. StatusResult *result = [StatusResult mj_objectWithKeyValues:dict_m8a];
  46. //打印博主信息
  47. for (Status *status in result.statuses) {
  48. NSString *text = status.text;
  49. NSString *name = status.user.name;
  50. NSString *icon = status.user.icon;
  51. NSLog(@"mj---text=%@, name=%@, icon=%@", text, name, icon);
  52. }
  53. //打印广告
  54. for (Ad *ad in result.ads) {
  55. NSLog(@"mj---image=%@, url=%@", ad.image, ad.url);
  56. }
  57. //打印结果
  58. //2016-07-04 13:47:58.994 PPDemos[3353:113055] mj---text=Nice weather!, name=Rose, icon=nami.png
  59. //2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---text=Go camping tomorrow!, name=Jack, icon=lufy.png
  60. //2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---image=ad01.png, url=http://www.ad01.com
  61. //2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---image=ad02.png, url=http://www.ad02.com

5.模型中的属性名和字典中的key不相同(或者需要多级映射)

  • 核心代码 mj_objectWithKeyValues:mj_replacedKeyFromPropertyName
    //多级映射,用点语法设置
    1. @interface Bag : NSObject
    2. @property (copy, nonatomic) NSString *name;
    3. @property (assign, nonatomic) double price;
    4. @end
    1. #import <Foundation/Foundation.h>
    2. @class Bag;
    3. @interface Student : NSObject
    4. @property (copy, nonatomic) NSString *ID;
    5. @property (copy, nonatomic) NSString *desc;
    6. @property (copy, nonatomic) NSString *nowName;
    7. @property (copy, nonatomic) NSString *oldName;
    8. @property (copy, nonatomic) NSString *nameChangedTime;
    9. @property (strong, nonatomic) Bag *bag;
    10. @end
    1. @implementation Student
    2. +(NSDictionary *)mj_replacedKeyFromPropertyName
    3. {
    4. // 实现这个方法的目的:告诉MJExtension框架模型中的属性名对应着字典的哪个key
    5. return @{
    6. @"ID" : @"id",
    7. @"desc" : @"desciption",
    8. @"oldName" : @"name.oldName",
    9. @"nowName" : @"name.newName",
    10. @"nameChangedTime" : @"name.info[1].nameChangedTime",
    11. @"bag" : @"other.bag"
    12. };
    13. }
    14. @end
    1. NSDictionry *dict_nokey = @{
    2. @"id" : @"20",
    3. @"desciption" : @"kids",
    4. @"name" : @{
    5. @"newName" : @"lufy",
    6. @"oldName" : @"kitty",
    7. @"info" : @[
    8. @"test-data",
    9. @{
    10. @"nameChangedTime" : @"2013-08"
    11. }
    12. ]
    13. },
    14. @"other" : @{
    15. @"bag" : @{
    16. @"name" : @"a red bag",
    17. @"price" : @100.7
    18. }
    19. }
    20. };
    21. //
    22. // // How to map
    23. // [Student mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
    24. // return @{
    25. // @"ID" : @"id",
    26. // @"desc" : @"desciption",
    27. // @"oldName" : @"name.oldName",
    28. // @"nowName" : @"name.newName",
    29. // @"nameChangedTime" : @"name.info[1].nameChangedTime",
    30. // @"bag" : @"other.bag"
    31. // };
    32. // }];
    33. // // Equals: Student.m implements +mj_replacedKeyFromPropertyName method.
    34. //字典转模型,支持多级映射
    35. Student *stu = [Student mj_objectWithKeyValues:dict_nokey];
    36. //打印
    37. NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
    38. stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
    39. NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
    40. //2016-07-04 14:20:28.082 PPDemos[3602:126004] ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
    41. //2016-07-04 14:20:28.082 PPDemos[3602:126004] bagName=a red bag, bagPrice=100.700000

6.将一个字典数组转成模型数组

  • 核心代码 mj_objectArrayWithKeyValuesArray
    1. NSArray *dictArray = @[
    2. @{
    3. @"name" : @"Jack",
    4. @"icon" : @"lufy.png"
    5. },
    6. @{
    7. @"name" : @"Rose",
    8. @"icon" : @"nami.png"
    9. }
    10. ];
    11. //字典数组转模型数组,使用的是mj_objectArrayWithKeyValuesArray:方法
    12. NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
    13. //打印
    14. for (User *user in userArray) {
    15. NSLog(@"name=%@, icon=%@", user.name, user.icon);
    16. }
    17. // name=Jack, icon=lufy.png
    18. // name=Rose, icon=nami.png

7. 将一个模型转成字典

  • 核心代码 mj_keyValues
    1. User *user = [[User alloc] init];
    2. user.name = @"Jack";
    3. user.icon = @"lufy.png";
    4. //
    5. Status *status = [[Status alloc] init];
    6. status.user = user;
    7. status.text = @"Nice mood!";
    8. //
    9. //模型转字典,使用的是mj_keyValues属性
    10. NSDictionary *statusDict = status.mj_keyValues;
    11. NSLog(@"%@", statusDict);
    12. /*
    13. {
    14. text = "Nice mood!";
    15. user = {
    16. icon = "lufy.png";
    17. name = Jack;
    18. };
    19. }
    20. */

8. 将一个模型数组转成字典数组

  • 核心代码 mj_keyValuesArrayWithObjectArray
    1. //创建模型数组
    2. User *user1 = [[User alloc] init];
    3. user1.name = @"Jack";
    4. user1.icon = @"lufy.png";
    5. User *user2 = [[User alloc] init];
    6. user2.name = @"Rose";
    7. user2.icon = @"nami.png";
    8. NSArray *userArray = @[user1, user2];
    9. //模型数组转字典数组,使用的是mj_keyValuesArrayWithObjectArray:方法
    10. NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
    11. NSLog(@"%@", dictArray);
    12. /*
    13. (
    14. {
    15. icon = "lufy.png";
    16. name = Jack;
    17. },
    18. {
    19. icon = "nami.png";
    20. name = Rose;
    21. }
    22. )
    23. */

更多用法

  1. 参考NSObject+MJKeyValue.h
  2. 参考NSObject+MJCoding.h
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/229221
推荐阅读
相关标签
  

闽ICP备14008679号