一、MJExtension第三方框架

我们在iOS开发过程中,我们常常需要将字典数据(也就是JSON数据)与Model模型之间的转化,例如网络请求返回的微博数据、等等,如果我们自己全部手动去创建模型并赋值,都是一些毫无技术含量的代码,费时费力,而且还可能会赋值出错,让我们很头疼。

MJExtension框架就是为了解决这个问题而设计得第三方开源库。这个开源库是之前传智博客的讲师李明杰老师写的,现在他自己出来做了,我iOS入门都是看李明杰老师的培训视频学习的,他讲得非常好,我非常喜欢他,他也算是我的老师了,他的作品我还是要学习下的。

提供了以下的一些方法实现:
  1. 简单的字典 --> 模型
  2. JSON字符串 --> 模型
  3. 复杂的字典 --> 模型 (模型里面包含了模型)
  4. 复杂的字典 --> 模型 (模型的数组属性里面又装着模型)
  5. 复杂的字典 --> 模型(模型属性名和字典的key不一样)
  6. 字典数组 --> 模型数组
  7. 模型 --> 字典
  8. 模型数组 --> 字典数组
  9. 字典 --> CoreData模型
  10. 归档与解档NSCoding
  11. 过滤字典的值

MJExtension框架是利用Obj-C的运行时机制编写的,现在iOS开发语言往Swift语言发展,我不太清楚Swift语言是否也有这种特性,该框架以后会不会在Swift语言上也发展下去不得而知,不过这个框架很轻量级,非常适合初级开发者去看它的源码,对理解Obj-C的运行时机制有非常大的帮助。

二、Runtime运行时机制简单了解

Runtime简称运行时,就是系统在运行的时候的一些机制,其中最主要的是消息机制

OC的函数调用类似于消息发送,属于动态调用过程。在编译的时候并不能决定真正调用哪个函数。事实证明,在编译阶段,OC可以调用任何函数,即使这个函数并未实现,只要申明过就不会报错。而C语言在编译阶段就会报错。只有在真正运行的时候才会根据函数的名称找到对应的函数来调用。

例如,下面的这个代码在编译时会被转化:
  1. /* OC方法调用 */
  2. [obj makeTest];
  3. /* 编译时Runtime会将上面的代码转为下面的消息发送 */
  4. objc_msgSend(obj, @selector(makeText));
iOS的顶层基类NSObject含有一个指向objc_class结构体的isa指针:
  1. @interface NSObject{
  2. Class isa;
  3. };
  4. typedef struct objc_class *Class;
  5. struct objc_class {
  6. Class isa; // 指向metaclass,也就是静态的Class
  7. Class super_class ; // 指向其父类
  8. const char *name ; // 类名
  9. long version ; // 类的版本信息,初始化默认为0
  10. /* 一些标识信息,如CLS_CLASS(0x1L)表示该类为普通class;
  11. CLS_META(0x2L)表示该类为metaclass */
  12. long info;
  13. long instance_size ; // 该类的实例变量大小(包括从父类继承下来的实例变量);
  14. struct objc_ivar_list *ivars; // 用于存储每个成员变量的地址
  15. /* 与info的一些标志位有关,如是普通class则存储对象方法,如是metaclass则存储类方法; */
  16. struct objc_method_list **methodLists ;
  17. struct objc_cache *cache; // 指向最近使用的方法的指针,用于提升效率;
  18. struct objc_protocol_list *protocols; // 存储该类遵守的协议
  19. };
objc_msgSend函数的调用过程:
  1. 首先通过obj的isa指针找到obj对应的Class。
  2. 在Class中先去cache中通过SEL查找对应函数method
  3. cache中未找到,再去methodLists中查找
  4. methodLists中未找到,则进入superClass按前面的步骤进行递归查找
  5. 若找到method,则将method加入到cache中,以方便下次查找,并通过method中的函数指针跳转到对应的函数中去执行。
  6. 如果一直查找到NSObject还没查找到,则会进入消息动态处理流程。
消息动态处理流程:
  1. /* 1. 时机处理之一,在这个方法中我们可以利用runtime的特性动态添加方法来处理 */
  2. + (BOOL)resolveInstanceMethod:(SEL)sel;
  3. /* 2. 时机处理之二,在这个方法中看代理能不能处理,如果代理对象能处理,则转接给代理对象 */
  4. - (id)forwardingTargetForSelector:(SEL)aSelector;
  5. /* 3. 消息转发之一,该方法返回方法签名,如果返回nil,则转发流程终止,抛出异常 */
  6. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
  7. /* 4. 消息转发之二,在该方法中我们可以对调用方法进行重定向 */
  8. - (void)forwardInvocation:(NSInvocation *)anInvocation;
所以使用Runtime机制我们就可以动态向类添加方法或属性:
  1. /* 动态向一个类添加属性 */
  2. class_addIvar(kclass, "expression", size, alignment, "*");
  3. /* 动态向一个类添加方法 */
  4. class_addMethod(kclass, @selector(setExpressionFormula:), (IMP)setExpressionFormula, "v@:@");
  5. class_addMethod(kclass, @selector(getExpressionFormula), (IMP)getExpressionFormula, "@@:");
  6. static void setExpressionFormula(id self, SEL cmd, id value){      
  7. NSLog(@"call setExpressionFormula");  
  8. }
  9. static id getExpressionFormula(id self, SEL cmd)  {        
  10. NSLog(@"call getExpressionFormula");  
  11. return nil;
  12. } 
  1. v表示void,@表示id类型,:表示SEL类型
  2. "v@:@":表示返回值为void,接受一个id类型、一个SEL类型、一个id类型的方法
  3. "@@:":表示返回值为id类型,接受一个id类型和一个SEL类型参数的方法

具体Runtime运行时使用细节,这里就不细讲,只是简单了解下Runtime是可以做到动态向类添加属性和方法就行。

三、MJExtension使用

MJExtension的大部分方法实现都集成到了分类上,不需要使用新的类,只需要包含头文件MJExtension.h即可。MJExtension在github上的使用说明已经写得十分明白了。

1. 简单的字典 --> 模型
模型类User定义:
  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
使用实例:
  1. NSDictionary *dict = @{
  2. @"name" : @"Jack",
  3. @"icon" : @"lufy.png",
  4. @"age" : @20,
  5. @"height" : @"1.55",
  6. @"money" : @100.9,
  7. @"sex" : @(SexFemale),/* 枚举需要使用NSNumber包装 */
  8. @"gay" : @"NO"
  9. };
  10. //字典转模型,使用的是mj_objectWithKeyValues:方法
  11. User *user = [User mj_objectWithKeyValues:dict];
2. JSON字符串 --> 模型
使用实例:
  1. // 定义一个JSON字符串
  2. NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
  3. // JSON字符串转模型
  4. User *user = [User mj_objectWithKeyValues:jsonString];
3. 复杂的字典 --> 模型 (模型里面包含了模型)
模型类Status定义:
  1. @interface Status : NSObject
  2. @property (copy, nonatomic) NSString *text;
  3. @property (strong, nonatomic) User *user;/* 其他模型类型 */
  4. @property (strong, nonatomic) Status *retweetedStatus;/* 自我模型类型 */
  5. @end
使用实例:
  1. NSDictionary *dict = @{
  2. @"text" : @"Agree!Nice weather!",
  3. @"user" : @{
  4. @"name" : @"Jack",
  5. @"icon" : @"lufy.png"
  6. },
  7. @"retweetedStatus" : @{
  8. @"text" : @"Nice weather!",
  9. @"user" : @{
  10. @"name" : @"Rose",
  11. @"icon" : @"nami.png"
  12. }
  13. }
  14. };
  15. //字典转模型,模型里面含有模型
  16. Status *status = [Status mj_objectWithKeyValues:dict];
  17. NSString *text = status.text;
  18. NSString *name = status.user.name;
  19. NSString *icon = status.user.icon;
  20. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  21. // text=Agree!Nice weather!, name=Jack, icon=lufy.png
  22. NSString *text2 = status.retweetedStatus.text;
  23. NSString *name2 = status.retweetedStatus.user.name;
  24. NSString *icon2 = status.retweetedStatus.user.icon;
  25. NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
  26. // text2=Nice weather!, name2=Rose, icon2=nami.png
4. 复杂的字典 --> 模型 (模型的数组属性里面又装着模型)
模型类Ad和StatusResult定义:
  1. @interface Ad : NSObject
  2. @property (copy, nonatomic) NSString *image;
  3. @property (copy, nonatomic) NSString *url;
  4. @end
  5. @interface StatusResult : NSObject
  6. /** 数组中存储模型Status类型数据 */
  7. @property (strong, nonatomic) NSMutableArray *statuses;
  8. /** 数组中存储模型Ad类型数据 */
  9. @property (strong, nonatomic) NSArray *ads;
  10. @property (strong, nonatomic) NSNumber *totalNumber;
  11. @end
  12. #import "MJExtension.h"
  13. /* 数组中存储模型数据,需要说明数组中存储的模型数据类型 */
  14. @implementation StatusResult
  15. /* 实现该方法,说明数组中存储的模型数据类型 */
  16. + (NSDictionary *)mj_ objectClassInArray{
  17. return @{ @"statuses" : @"Status",
  18.               @"ads" : @"Ad"
  19.             };
  20. }
  21. @end
使用实例:
  1. NSDictionary *dict = @{
  2. @"statuses" : @[
  3. @{
  4. @"text" : @"Nice weather!",
  5. @"user" : @{
  6. @"name" : @"Rose",
  7. @"icon" : @"nami.png"
  8. }
  9. },
  10. @{
  11. @"text" : @"Go camping tomorrow!",
  12. @"user" : @{
  13. @"name" : @"Jack",
  14. @"icon" : @"lufy.png"
  15. }
  16. }
  17. ],
  18. @"ads" : @[
  19. @{
  20. @"image" : @"ad01.png",
  21. @"url" : @"http://www.ad01.com"
  22. },
  23. @{
  24. @"image" : @"ad02.png",
  25. @"url" : @"http://www.ad02.com"
  26. }
  27. ],
  28. @"totalNumber" : @"2014"
  29. };
  30. //字典转模型,支持模型的数组属性里面又装着模型
  31. StatusResult *result = [StatusResult mj_objectWithKeyValues:dict];
  32. //打印博主信息
  33. for (Status *status in result.statuses) {
  34. NSString *text = status.text;
  35. NSString *name = status.user.name;
  36. NSString *icon = status.user.icon;
  37. NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
  38. }
  39. // text=Nice weather!, name=Rose, icon=nami.png
  40. // text=Go camping tomorrow!, name=Jack, icon=lufy.png
  41. //打印广告
  42. for (Ad *ad in result.ads) {
  43. NSLog(@"image=%@, url=%@", ad.image, ad.url);
  44. }
  45. // image=ad01.png, url=http://www.ad01.com
  46. // image=ad02.png, url=http://www.ad02.com
5. 复杂的字典 --> 模型(模型属性名和字典的key不一样)
模型类Bag和Student定义:
  1. @interface Bag : NSObject
  2. @property (copy, nonatomic) NSString *name;
  3. @property (assign, nonatomic) double price;
  4. @end
  5. @interface Student : NSObject
  6. @property (copy, nonatomic) NSString *ID;
  7. @property (copy, nonatomic) NSString *desc;
  8. @property (copy, nonatomic) NSString *nowName;
  9. @property (copy, nonatomic) NSString *oldName;
  10. @property (copy, nonatomic) NSString *nameChangedTime;
  11. @property (strong, nonatomic) Bag *bag;
  12. @end
  13. #import "MJExtension.h"
  14. @implementation
  15. /* 设置模型属性名和字典key之间的映射关系 */
  16. + (NSDictionary *)mj_replacedKeyFromPropertyName{
  17. /* 返回的字典,key为模型属性名,value为转化的字典的多级key */
  18. return @{
  19. @"ID" : @"id",
  20. @"desc" : @"desciption",
  21. @"oldName" : @"name.oldName",
  22. @"nowName" : @"name.newName",
  23. @"nameChangedTime" : @"name.info[1].nameChangedTime",
  24. @"bag" : @"other.bag"
  25. };
  26. }
  27. @end
使用实例:
  1. NSDictionary *dict = @{
  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. Student *stu = [Student mj_objectWithKeyValues:dict];
  23. //打印
  24. NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
  25. stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
  26. // ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
  27. NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
  28. // bagName=a red bag, bagPrice=100.700000
6. 字典数组 --> 模型数组
使用实例:
  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. 模型 --> 字典
使用实例:
  1. //创建一个模型对象
  2. User *user = [[User alloc] init];
  3. user.name = @"Jack";
  4. user.icon = @"lufy.png";
  5. Status *status = [[Status alloc] init];
  6. status.user = user;
  7. status.text = @"Nice mood!";
  8. //模型转字典,使用的是mj_keyValues属性
  9. NSDictionary *statusDict = status.mj_keyValues;
  10. NSLog(@"%@", statusDict);
  11. /*
  12.  {
  13.  text = "Nice mood!";
  14.  user =     {
  15.  icon = "lufy.png";
  16.  name = Jack;
  17.  };
  18.  }
  19.  */
8. 模型数组 --> 字典数组
使用实例:
  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. */
9. 字典 --> CoreData模型
使用实例:
  1. NSDictionary *dict = @{
  2. @"name" : @"Jack",
  3. @"icon" : @"lufy.png",
  4. @"age" : @20,
  5. @"height" : @1.55,
  6. @"money" : @"100.9",
  7. @"sex" : @(SexFemale),
  8. @"gay" : @"true"
  9. };
  10. //字典转为CoreData模型
  11. NSManagedObjectContext *context = nil;
  12. User *user = [User mj_objectWithKeyValues:dict
  13. context:context];
  14. [context save:nil];
10. 归档与解档NSCoding
模型类Bag添加实现:
  1. @interface Bag : NSObject <NSCoding>
  2. @property (copy, nonatomic) NSString *name;
  3. @property (assign, nonatomic) double price;
  4. @end
  5. #import "MJExtension.h"
  6. @implementation Bag
  7. //添加了下面的宏定义
  8. MJExtensionCodingImplementation
  9. /* 实现下面的方法,说明哪些属性不需要归档和解档 */
  10. + (NSArray *)mj_ignoredCodingPropertyNames{
  11. return @[@"name"];
  12. }
  13. @end
使用实例:
  1. //创建模型
  2. Bag *bag = [[Bag alloc] init];
  3. bag.name = @"Red bag";
  4. bag.price = 200.8;
  5. //获取归档路径
  6. NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"];
  7. //归档
  8. [NSKeyedArchiver archiveRootObject:bag toFile:file];
  9. //解档
  10. Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
  11. NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
  12. // name=(null), price=200.800000
11. 过滤字典的值
模型类Book实现:
  1. @interface Book: NSObject
  2. @property (copy, nonatomic) NSString *name;
  3. @property (strong, nonatomic) NSDate *publishedTime;
  4. @end
  5. #import "MJExtension.h"
  6. @implementation Book
  7. /* 转化过程中对字典的值进行过滤和进一步转化 */
  8. - (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
  9. {
  10. if ([property.name isEqualToString:@"publisher"]) {
  11. if (oldValue == nil) {
  12. return @"";
  13. }
  14. } else if (property.type.typeClass == [NSDate class]) {
  15. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  16. fmt.dateFormat = @"yyyy-MM-dd";
  17. return [fmt dateFromString:oldValue];
  18. }
  19. return oldValue;
  20. }
  21. @end
使用实例:
  1. NSDictionary *dict = @{
  2. @"name" : @"5分钟突破iOS开发",
  3. @"publishedTime" : @"2011-09-10"
  4. };
  5. //字典转模型,过滤name为nil的情况,把NSString转为NSDate
  6. Book *book = [Book mj_objectWithKeyValues:dict];
  7. //打印
  8. NSLog(@"name=%@, publishedTime=%@", book.name, book.publishedTime);

iOS学习笔记38-MJExtension使用的更多相关文章

  1. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  2. IOS学习笔记25—HTTP操作之ASIHTTPRequest

    IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...

  3. IOS学习笔记之关键词@dynamic

    IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...

  4. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  5. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  6. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  7. iOS学习笔记之Category

    iOS学习笔记之Category 写在前面 Category是类别(也称为类目或范畴),使用Category,程序员可以为任何已有的类添加方法.使用类别可以对框架提供的类(无法获取源码,不能直接修改) ...

  8. iOS学习笔记之ARC内存管理

    iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...

  9. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  10. IOS学习笔记07---C语言函数-printf函数

    IOS学习笔记07---C语言函数-printf函数 0 7.C语言5-printf函数 ------------------------- ----------------------------- ...

随机推荐

  1. 2015 ACM/ICPC Asia Regional Changchun Online Pro 1002 Ponds(拓扑排序+并查集)

    Ponds Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Sub ...

  2. UVA 536 TreeRocvery 树重建 (递归)

    根据先序历遍和中序历遍输出后序历遍,并不需要真的建树,直接递归解决 #include<cstdio> #include<cstring> ; char preOrder[N]; ...

  3. Ubuntu18.04如何从英文界面更改为中文界面

    本文介绍如何将Ubuntu18.04安装后的英文界面,更改为中文界面,即系统语言由英文改为简体中文.注意,与安装中文输入法不同,两者也没有冲突. 首先进入设置(Setting),选择区域和语言(Reg ...

  4. 使用Python-OpenCV向图片添加噪声(高斯噪声、椒盐噪声)

    在matlab中,存在执行直接得函数来添加高斯噪声和椒盐噪声.Python-OpenCV中虽然不存在直接得函数,但是很容易使用相关的函数来实现. 代码: import numpy as np impo ...

  5. JEECMS开发问题汇总

    1 添加Controller 将controller文件放在com.jeecms.cms.action.front包中, 图1.1 然后在jeecms-servlet-front-action.xml ...

  6. 2018.4.10 Ubuntu cat命令解答、用法

    cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从键盘创建一个文件. $ cat > ...

  7. RSA AES 前端JS与后台JAVA的加密解密的是实现

    AES CryptoJS 前提是编码方式,key,vi中设置一样,就可以进行跨语言加密解密 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ...

  8. Object类 任何类都是object类的子类 用object对象接收数组 object类的向上向下转型

    任何类都是object类的子类 用object对象接收数组 object类的向上向下转型

  9. VueX源码分析(1)

    VueX源码分析(1) 文件架构如下 /module /plugins helpers.js index.esm.js index.js store.js util.js util.js 先从最简单的 ...

  10. python queue - 同步队列类

    参考 官网 queue 模块 queue 模块实现多生产者,多消费者队列. 当必须在 ==多个线程之间安全地交换信息== 时,它在线程编程中特别有用. 此模块中的Queue类实现了所有必需的锁定语义. ...