OC中第三方库MJExtension的使用
MJExtension是一套常用的"字典和模型之间互相转换"的框架,在项目中也使用过,现在记录一下。随着Swift的普及,在Swift中也有一个类似功能的框架HandyJSON 也非常好用。有空我也会介绍一下这个框架。
MJExtension 能完成的功能
<1> 字典转模型
<2>模型转字典
<3>字典数组->模型数组
<4>模型数组->字典数组
一 字典转模型
//字典转模型
- (void) dicToModel {
//简单的字典
NSDictionary *dict_user = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @,
@"height" : @"1.55",
@"money" : @100.9,
@"sex" : @(SexFemale),/* 枚举需要使用NSNumber包装 */
@"gay" : @YES
};
User *user = [User mj_objectWithKeyValues:dict_user];
NSLog(@"%@ %u %@",user.name,user.sex,user.icon); }
二 JSON字符串转模型
- (void) stringToModel {
NSString *jsonStr = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
User *user = [User mj_objectWithKeyValues:jsonStr];
NSLog(@"%@ %u %@",user.name,user.age,user.icon);
}
三 复杂的字典转模型
@interface Status : NSObject @property (nonatomic,copy) NSString *text;
@property (nonatomic,strong) User *user;
@property (nonatomic,strong) Status *retweetedStatus; @end - (void)complexDicToModel {
NSDictionary *dict_m8m = @{
@"text" : @"Agree!Nice weather!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"Nice weather!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
}; Status *status = [Status mj_objectWithKeyValues:dict_m8m];
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"mj-----text=%@, name=%@, icon=%@", text, name, icon);
NSString *text2 = status.retweetedStatus.text;
NSString *name2 = status.retweetedStatus.user.name;
NSString *icon2 = status.retweetedStatus.user.icon;
NSLog(@"mj-----text2=%@, name2=%@, icon2=%@", text2, name2, icon2); }
四 模型中有个数组属性,数组里面又装着其他属性
@interface Status : NSObject @property (nonatomic,copy) NSString *text;
@property (nonatomic,strong) User *user;
@property (nonatomic,strong) Status *retweetedStatus; @end @interface ADModel : NSObject @property (nonatomic,copy) NSString *image; @property (nonatomic,copy) NSString *url; @end @interface resultModel : NSObject @property (nonatomic,strong) NSMutableArray *statuses;
@property (nonatomic,strong) NSMutableArray *ads;
@property (nonatomic,strong) NSNumber *totalNumber;
@property (nonatomic,assign) long long previousCursor;
@property (nonatomic,assign) long long nextCursor; @end @implementation resultModel + (NSDictionary *)mj_objectClassInArray {
return @{@"statuses" : @"Status", @"ads":@"ADModel"};
} @end - (void)complexDicContentArrToModel {
// 1.定义一个字典
NSDictionary *dict = @{
@"statuses" : @[
@{
@"text" : @"今天天气真不错!", @"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}, @{
@"text" : @"明天去旅游了", @"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
} ], @"ads" : @[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.小码哥ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.小码哥ad02.com"
}
], @"totalNumber" : @"",
@"previousCursor" : @"",
@"nextCursor" : @""
};
resultModel *model = [resultModel mj_objectWithKeyValues:dict];
NSLog(@"resultModel %lld ",model.nextCursor,model.previousCursor);
// 4.打印statuses数组中的模型属性
for (Status *status in model.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
MJExtensionLog(@"text=%@, name=%@, icon=%@", text, name, icon);
} // 5.打印ads数组中的模型属性
for (ADModel *ad in model.ads) {
MJExtensionLog(@"image=%@, url=%@", ad.image, ad.url);
} }
五 简单的字典转模型 (key替换 比如ID和id,支持多级映射)
@interface Bag : NSObject @property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) CGFloat price; @end @interface Student : NSObject @property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *otherName;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (copy, nonatomic) NSString *desc;
@property (strong, nonatomic) Bag *bag; @end @implementation Student + (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{@"ID":@"id",@"desc":@"desciption",@"oldName":@"name.oldName",@"nowName":@"name.newName",@"nameChangedTime":@"name.info[1].nameChangedTime",@"bag":@"other.bag"};
} @end - (void)keyValues2object4 {
// 1.定义一个字典
NSDictionary *dict = @{
@"id" : @"",
@"desciption" : @"好孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @[
@"test-data",
@{@"nameChangedTime" : @"2013-08-07"}
]
},
@"other" : @{
@"bag" : @{
@"name" : @"小书包",
@"price" : @100.7
}
}
}; // 2.将字典转为MJStudent模型
Student *stu = [Student mj_objectWithKeyValues:dict]; // 3.打印MJStudent模型的属性
MJExtensionLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@", stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
MJExtensionLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
}
六 将一个字典数组转成模型数组
- (void)arrayToModel {
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
}
七 将一个模型转成字典
- (void)modelToDict {
User *user = [[User alloc] init];
user.name = @"jack";
user.icon = @"lufy.png";
NSDictionary *userDic = user.mj_keyValues;
NSLog(@"%@",userDic);
}
八 将一个模型数组转成字典数组
//模型数组 转 字典数组
- (void)modelArrayToDicArray {
User *user1 = [[User alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";
User *user2 = [[User alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";
NSArray *userArray = @[user1, user2]; NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray]; }
九 NSCoding 示例
#import <Foundation/Foundation.h>
#import "MJExtension.h" @interface LFCar : NSObject @property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) double price; @end #import "LFCar.h" @implementation LFCar MJExtensionCodingImplementation @end - (void)viewDidLoad {
[super viewDidLoad]; LFCar *car = [[LFCar alloc] init];
car.name = @"red bag";
car.price = 200.8; NSString *file = [NSTemporaryDirectory() stringByAppendingPathComponent:@"bag.data"];
//归档
[NSKeyedArchiver archiveRootObject:car toFile:file];
//解档
LFCar *decodedCar = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
NSLog(@"%@ %f",decodedCar.name,decodedCar.price);
}
这就是MJExtension 的基本用法 还有一些其他的不常用的用法 参考这里http://blog.csdn.net/jeikerxiao/article/details/51590222
OC中第三方库MJExtension的使用的更多相关文章
- Python中第三方库Requests库的高级用法详解
Python中第三方库Requests库的高级用法详解 虽然Python的标准库中urllib2模块已经包含了平常我们使用的大多数功能,但是它的API使用起来让人实在感觉不好.它已经不适合现在的时代, ...
- 在swift调用OC的第三方库
https://www.jianshu.com/p/4799ac1d7dce 2017.06.02 23:55* 字数 275 阅读 1619评论 0喜欢 3 环境:xcode 8.3.2 系统: M ...
- 用CMake设置Visual Studio工程中第三方库
较大的工程文件中一般会调用动态库或者静态库,如果这些库文件是当前工程包含的项目,CMake会自动识别并添加Debug和Release编译时需要的库文件路径和文件名,可以使用命令: Target_Lin ...
- Python中第三方库的安装
网上的帖子挺多的,教你如何安装,安装第三方工具库的方法总共分为三类:Dos系统下pip命令:安装包下载安装:IDE集成环境下安装(Pycharm,Spyder……) http://www.jiansh ...
- 关于python中第三方库安装方法和问题解决
一.安装方法 方法一: 1.管理员身份启动命令行(运行--->cmd) 2.pip install 库的绝对路径和库的详细名称 :或者运用cd命令跳转到下载好的库所在的位置然后pip insta ...
- yii中第三方库
yii中存在一些路径别名:ext:表示包含了所有第三方扩展的目录 参考:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.namespac ...
- ios中第三方库归结
1:uiscrollview 折叠 展开中不包含tablecell. 展开列表效果 Collapse Click () https://github.com/bennyguitar/Collapse ...
- Swift中混编OC第三方库
现在Swift的第三方库还比较少,有时候需要使用OC的第三方库,其实也是很容易的. 我们使用如下步骤: 1.新建的Swift项目,第一次创建OC文件时会询问是否生成 桥接头,选择是的话会生成一个桥 ...
- swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者
Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...
随机推荐
- .net平台 基于 XMPP协议的即时消息服务端简单实现
.net平台 基于 XMPP协议的即时消息服务端简单实现 昨天抽空学习了一下XMPP,在网上找了好久,中文的资料太少了所以做这个简单的例子,今天才完成.公司也正在准备开发基于XMPP协议的即时通讯工具 ...
- 如何Tomcat注册成系统服务
1.开始->运行(windos+r)中敲cmd,DOS界面2.cd到tomcat的bin目录下3.运行命令service.bat install 这里可以指定注册服务的名字,然后就可以 ...
- Windows虚拟内存如何设置
当我们在运行一些大型的软件,或者是刚刚退出游戏的时候经常会提示"你的虚拟内存过低"的提示,出现这种情况一般是:一:你的物理内存比较小,运行大的软件比较吃力:二:你运行了许多窗口或者 ...
- Linux 常见安全检查方法
Linux 常见安全检查方法进行概要说明: 一.检查系统密码文件,查看文件修改日期 # ls -l /etc/passwd 二.查看 passwd 文件中有哪些特权用户 # awk -F: '$3= ...
- HTML 5 音频Audio
在HTML5标准网页里面,我们能够运用audio标签来完毕我们对声音的调用及播放. 下面是最常常见到的运用HTML5三种基本格式: 1.最少的代码 <audio src="song.o ...
- 如何在Django1.6结合Python3.3版本中使用MySql
用起了Python3.4跟Django1.6,数据库依然是互联网企业常见的MySql. 悲催的是在Python2.7时代连接MySql的MySQLdb还不支持Python3.4,还好,苦苦追问G哥终于 ...
- Linux 下Office 软件名称
Linux 下Office 软件名称
- hbase和mapreduce开发 WordCount
代码: /** * hello world by world 测试数据 * @author a * */ public class DefinedMapper extends Mapper<Lo ...
- Debian Epson L455 打印机
要使用打印机必须要在本机(或局域网其他机器)上安装打印服务,L455是网络打印机,需要IPP协议,而mDNS-scan和avahi-utils是用来发现网络打印机的服务,由于我是摸索着安装的,没太研究 ...
- centos关机与重启命令详解
Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdo ...