ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用
一:url转码,当url中涉及到中文的时候,要考虑转码,用UTF8对中文的url进行转码
#import "ViewController.h" @interface ViewController () @end @implementation ViewController #pragma mark ----------------------
#pragma mark Events
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self post];
} #pragma mark ----------------------
#pragma mark Methods
-(void)get
{ NSString *urlStr = @"http://120.25.226.186:32812/login2?username=小码哥&pwd=520it&type=JSON"; NSLog(@"转码前: %@",urlStr); //中文转码处理
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSLog(@"转码后: %@",urlStr); //1.url
NSURL *url = [NSURL URLWithString:urlStr]; //http://120.25.226.186:32812/login2?username=%E5%B0%8F%E7%A0%81%E5%93%A5&pwd=520it&type=JSON NSLog(@"url------%@",url); //2.urlrequest
NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.connect
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //容错处理
if (connectionError) {
NSLog(@"%@",connectionError);
return ;
}
//4.解析
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}]; } -(void)post
{
//观察URL中是否有中文,如果有中文则需要转码
NSString *urlStr = @"http://120.25.226.186:32812/login2"; //username=小码哥&pwd=520it&type=JSON
//1.url
NSURL *url = [NSURL URLWithString:urlStr]; //2.urlrequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; //2.1 post
request.HTTPMethod = @"POST"; //2.2 body
request.HTTPBody = [@"username=小码哥&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding]; //3.connect
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { //容错处理
if (connectionError) {
NSLog(@"%@",connectionError);
return ;
}
//4.解析
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}]; }
@end
二:MJExtension的使用
1:字典转模型框架
1)相关框架
a.Mantle 需要继承自MTModel
b.JSONModel 需要继承自JSONModel
c.MJExtension 不需要继承,无代码侵入性
(2)自己设计和选择框架时需要注意的问题
a.侵入性
b.易用性,是否容易上手
c.扩展性,很容易给这个框架增加新的功能
(3):MJExtension的使用
1:字典转模型:
1://file:是读取本地的数据本地的资源包存在NSBundle中,url是获取网络端的数据
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"person" ofType:@"plist"]];
XMGPerson *person = [XMGPerson mj_objectWithKeyValues:dict];
2:当网络数据既有字典又有数组字典的时候:需要在模型中重写:
+ (NSDictionary *)mj_objectClassInArray方法。以下两种方试都可以
+ (NSDictionary *)mj_objectClassInArray
{
// key : 属性名
// value : 类名
return @{
@"dogs" : @"XMGDog",
@"books" : @"XMGBook"
};
// return @{
// @"dogs" : [XMGDog class],
// @"books" : [XMGBook class]
// };
}
3:当需要改变网络返回数据的key值时:如遇到系统关键字id或是description时,需要在模型中重写
+ (NSDictionary *)mj_replacedKeyFromPropertyName
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
// 属性名-字典key-映射
// key : 属性名
// value : 字典中的key
return @{
@"desc" : @"description",
@"ID" : @"id",
@"name" : @[@"name", @"screenname"],//表示属性name既可以对应name也可以对应screenname,优先查看name的值若是name的值没有,则查看screenname的值
@"info" : @[@"other.info[1].abc",//此种情况的数据情况如图:点语法是为字典的key赋值,[]表示数组中的第几个对象,.abc表示这个对象下的test
@"other.info[0].test",
@"name"]
};
}

注意:如果服务器返回的数据最外层是字典,字典中又有字典A,字典A中又有其他字典,数据结构如图:

解决办法:1:可以用字典转模型,将other字典再构造一个模型 ,字典转模型 2:还可以在数据model中重写+ (NSDictionary *)mj_replacedKeyFromPropertyName方法,
+ (NSDictionary *)mj_replacedKeyFromPropertyName
{
// 属性名-字典key-映射
// key : 属性名
// value : 字典中的key
return @{
@"desc" : @"description",
@"ID" : @"id",
@"name" : @[@"name", @"screenname"],//表示属性name既可以对应name也可以对应screenname,优先查看name的值若是name的值没有,则查看screenname的值
@"info" : @"other.info"
};
}
4:MJExtension在项目中的应用:一般为了减少 MJExtension对项目的侵入性,基本做法都是新建配置类继承NSObject,在配置类中实现+(void)load方法,在此方法中对模型中的字段进行配置
#import "XMGExtensionConfig.h"
#import "XMGUser.h"
#import "XMGBook.h"
#import "XMGDog.h"
#import <MJExtension.h>
#import "XMGOther.h"
#import "XMGPerson.h" @implementation XMGExtensionConfig + (void)load
{
#pragma mark - XMGUser
[XMGUser mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{
@"desc" : @"description",
// @"ID" : @"id",
@"name" : @[@"name", @"screenname"],
@"info" : @[@"other.info[1].abc",
@"other.info[0].test",
@"name"]
};
}]; [XMGUser mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"dogs" : @"XMGDog",
@"books" : @"XMGBook"
};
}]; #pragma mark - XMGPerson
[XMGPerson mj_setupReplacedKeyFromPropertyName121:^NSString *(NSString *propertyName) {
// if ([propertyName isEqualToString:@"ID"]) return @"id";
return [propertyName mj_underlineFromCamel];
}]; #pragma mark - XMGBook
// [XMGBook mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
// return @{@"ID" : @"id"};
// }]; #pragma mark - XMGDog
// [XMGDog mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
// return @{@"ID" : @"id"};
// }]; [NSObject mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"ID" : @"id"};
}];
} @end
注意:1:在配置类中进行数据模型的配置,需要实现+(void)load方法,在此方法内进行配置,不需要导入头文件,因为+(void)load是在类加载进内存就会被调用,而且只会被调用一次,+(void)initialize 是累呗使用的时候调用,也是被调用一次 2:若是向一些参数模型,或是数据模型等含有重复的字段值,1:则可以抽重复的字段值抽成父类,让子类去继承,此时父类也可以提供初始化方法给子类继承,在父类初始化方法中父类可以为一些固定的属性赋值 2:用父类去进行初始化,也就是如上面代码所示,都含有ID的配置,则可以用父类NSObject去配置,因为任何类都是继承自NSObject,则此时子类就相当于配置了ID,它的原理就是oc的对象机制,先优先从子类中寻找,子类要是有此属性,就不再去父类寻找,子类若是没有此属性,则其会去父类中查找。
ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用的更多相关文章
- ios开发网络学习三:NSURLConnection小文件大文件下载
一:小文件下载 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDele ...
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...
- ios开发网络学习十二:NSURLSession实现文件上传
#import "ViewController.h" // ----WebKitFormBoundaryvMI3CAV0sGUtL8tr #define Kboundary @&q ...
- ios开发网络学习:一:NSURLConnection发送GET,POST请求
#import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelegate> ...
- ios开发网络学习AFN框架的使用一:get和post请求
#import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...
- ios开发网络学习九:NSURLSessionDownloadTask实现大文件下载
一:NSURLSessionDownloadTask:实现文件下载:无法监听进度 #import "ViewController.h" @interface ViewControl ...
- ios开发网络学习五:输出流以及文件上传
一:输出流 #import "ViewController.h" @interface ViewController ()<NSURLConnectionDataDelega ...
- ios开发runtime学习二:runtime交换方法
#import "ViewController.h" /* Runtime(交换方法):主要想修改系统的方法实现 需求: 比如说有一个项目,已经开发了2年,忽然项目负责人添加一个功 ...
- ios开发网络学习十一:NSURLSessionDataTask离线断点下载(断点续传)
#import "ViewController.h" #define FileName @"121212.mp4" @interface ViewControl ...
随机推荐
- 安卓使用WebView下载文件,安卓实现软件升级功能
由于调用系统默认浏览器下载更新,造成用户体验非常不好,所以决定在webview中直接下载系统更新.然后直接安装. 由于要下载,所以必须用webview,联网权限这里不说了,直接写在manifafest ...
- email之TO、CC、BCC意义
CC 英文全称是 Carbon Copy(抄送); BCC英文全称是 Blind CarbonCopy(暗抄送). 两者的差别在于在BCC栏中的收件人能够看到全部的收件人名(TO,CC,BCC),而在 ...
- emacs 为什么找不到运行程序?
我记得前段时间有个朋友问我为什么在emacs中打不开matlab程序?明明在terminal下是能打开的,却在emacs中不行. 今天自己最终遇到了相似的问题,我今天安装racket 6.0.安装好后 ...
- 使WordPress改域名后网站正常运行的方法
使WordPress改域名后网站正常运行的方法 wp-content/wp-config.php $path = '/blog'; $scheme = (isset($_SERVER['HTTPS'] ...
- 给指定的用户无需密码执行 sudo 的权限
给指定的用户无需密码执行 sudo 的权限 cat /etc/passwd 可以查看所有用户的列表 w 可以查看当前活跃的用户列表 cat /etc/group 查看用户组 cat /etc/pass ...
- 2.5 Legacy APIs官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.5 Legacy APIs A more limited legacy prod ...
- php 生成文件txt到指定目录
// 向文件追加写入内容 $site = PHP_EOL.date("Y-m-d H:i:s")."world"; //PHP_EOL换行 // 使用 FILE ...
- 趣闻|Python之禅(The Zen of Python)
在Python解释器中输入“import this”会发生什么?如果你不知道这个彩蛋,推荐继续阅读这篇文章. 2001年秋,Foretec(一家会议组织公司)正在准备召开第十届Internationa ...
- linux创建新用户并给予root权限
root比windows的系统管理员的能力更大,足以把整个系统的大部分文件删掉,导致系统完全毁坏,不能再次使用.所以,用root进行不当的操作是相当危险的,轻微的可以死机,严重的甚至不能开机.所以,在 ...
- 【Educational Codeforces Round 35 D】Inversion Counting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排列中交换任意两个数字. 排列的逆序对个数的奇偶性会发生变化. 翻转这个过程其实就是len/2对数字发生交换. 交换了偶数次的话,不 ...