iOS开发技术iOS学习笔记

“URL小能手”NSURLComponents

2019-08-07  本文已影响106人  lucky_lee
背景
if ([urlPath rangeOfString:@"?"].location == NSNotFound) {
    urlPath = [urlPath stringByAppendingFormat:@"?orderId=%@&type=%lu&lang=%@&cityId=%ld", orderId,type,lang,cityId];
} else {
    urlPath = [urlPath stringByAppendingFormat:@"orderId=%@&type=%lu&lang=%@&cityId=%ld", orderId,type,lang,cityId];
}

诸如此类的代码,不知道有多少小伙伴踩过坑~

那么这个时候如果使用NSURLComponents进行组合和分解将会事半功倍。

相关知识延伸

在一维数据类型中,URI占据了至高无上的地位。这里,在单个的,可解析的字符串中,是编码在计算机上具有,确实和将要存在的任何信息的位置所必需的每条可想到的信息。

在最基本的形式中,URI由方案名称和层次结构部分组成,带有可选的查询和片段:

<scheme name> : <hierarchical part> [ ? <query> ] [ # <fragment> ]

许多协议(包括HTTP)为层次结构部分中的用户名,密码,端口和路径等信息指定常规结构:

网址结构

扎实地掌握网络编程,做到对URL组件的熟悉。

NSURLComponents

NSURLComponents 在iOS 7.0和macOS 10.9中引入的,所以从时间上看已经是存在一段时间了。
要创建NSURLComponents对象,您可以使用a String或an URL

正如官方文档所描述:

If resolvingAgainstBaseURL is true and url is a relative URL, the components of url.absoluteURL are used. If the url string from the URL is malformed, nil is returned.

下面我们就根据下面的代码,进行一个简单的介绍分析

NSString * urlString = @"http://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image";
NSURLComponents * urlComponents = [[NSURLComponents alloc]initWithString:urlString];
NSLog(@"urlComponents.host = \n%@",urlComponents.host);//>>image.baidu.com
NSLog(@"urlComponents.scheme = \n%@",urlComponents.scheme);//>>http
NSLog(@"urlComponents.path = \n%@",urlComponents.path);//>>/search/index
NSLog(@"urlComponents.query = \n%@",urlComponents.query);//>>tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image

这个时候大家会不会就有眼前一亮的感觉😁
如果在配合下面一句呢

 NSLog(@"urlComponents.ququeryItems=%@",urlComponents.queryItems);//>>(
    "<NSURLQueryItem 0x6000016fe720> {name = tn, value = baiduimage}",
    "<NSURLQueryItem 0x6000016fe740> {name = ps, value = 1}",
    "<NSURLQueryItem 0x6000016fe760> {name = ct, value = 201326592}",
    "<NSURLQueryItem 0x6000016fe780> {name = lm, value = -1}",
    "<NSURLQueryItem 0x6000016fe7a0> {name = cl, value = 2}",
    "<NSURLQueryItem 0x6000016fe7c0> {name = nc, value = 1}",
    "<NSURLQueryItem 0x6000016fe7e0> {name = ie, value = utf-8}",
    "<NSURLQueryItem 0x6000016fe800> {name = word, value = image}"
)

可以看出,为我们提供了一个NSURLQueryItem,各个查询项的键/值对象,这使我们更容易追加查询参数,或检查URL是否包含查询参数。

按照这个思路,各位小伙伴,现在是不是可以放飞自我了😏,对已有的URL进行尽情的组合,分解。

拼装
NSURLQueryItem * item = [[NSURLQueryItem alloc]initWithName:@"imageSize" value:@"1024*1024"];
NSMutableArray * items = [NSMutableArray arrayWithArray:urlComponents.queryItems];
[items addObject:item];
urlComponents.queryItems = [items copy];
NSLog(@"urlComponents.ququeryItems=%@",urlComponents.queryItems);//>>(
    "<NSURLQueryItem 0x6000037712a0> {name = tn, value = baiduimage}",
    "<NSURLQueryItem 0x6000037712c0> {name = ps, value = 1}",
    "<NSURLQueryItem 0x6000037712e0> {name = ct, value = 201326592}",
    "<NSURLQueryItem 0x600003771300> {name = lm, value = -1}",
    "<NSURLQueryItem 0x600003771320> {name = cl, value = 2}",
    "<NSURLQueryItem 0x600003771340> {name = nc, value = 1}",
    "<NSURLQueryItem 0x600003771360> {name = ie, value = utf-8}",
    "<NSURLQueryItem 0x600003771380> {name = word, value = image}",
    "<NSURLQueryItem 0x6000037713a0> {name = imageSize, value = 1024*1024}"
)

再来打印一下urlComponents.query

NSLog(@"urlComponents.query = \n%@",urlComponents.query);//>>tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=image&imageSize=1024*1024

简单而美观,所有的辛勤工作都被委托给NSURLComponents我们甚至不用担心是否使用?&...

分解

如上述代码,将数据遍历,就可以拿到单个的NSURLQueryItem,此时咱们可以处理 包含、去重等等操作,现在我们知道NSURLQueryItems检查查询参数是否存在太容易了。😁

现在在回头看,现在的处理是不是更加的优雅,便于维护,便于扩展了呢。

如果你想更好的运用和深入地了解,可以去apple官方文档查阅。

参考资料
上一篇下一篇

猜你喜欢

热点阅读