NSURL类
来源:互联网 发布:js unicode转中文 编辑:程序博客网 时间:2024/06/14 09:25
文章转自http://haoxiang.org/2011/10/nsurl/
码农Coding的时候有各种不好的习惯。
比如,不喜欢好好地看框架的文档,一旦找到某一个看起来简单易懂的接口,就一直用它。
如果需要之后的处理,往往简单粗暴。
我们以前管这种情况叫做“裸”。最近又不知不觉地写了比较“裸”的代码。
说实话,这个习惯得改,“裸”写的东西往往不健壮,不可读,效率还不高。成熟框架提供的直接可用的接口必须是第一选择。
NSURL是常用的类,用来描述一段URL的。
需要取得URL中不同部分的时候,我们应该用URL提供的接口,
而不是把它当做一个普通的字符串去手工分析。
比如:
http://www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456
NSURL
*url = [
NSURL
URLWithString:
@"http:/www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456"
];
下面是常用的几个接口,和它们的输出。接口意思都符合相关RFC里的定义。
[url scheme]
http
[url host]
www.testurl.com
[url port]
8080
[url path]
/subpath/subsubpath
[url lastPathComponent]
subsubpath
[url query]
uid=123&gid=456
NSURL 英文版 :]
NSURL is widely used, but sometimes we are not following the idiomatic usage.
When it comes to access part of the url, it is better to use the methods NSURL provide, instead of analysis its absoluteString manually.
Take this one for example:
http://www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456
NSURL *url = [NSURL URLWithString:@"http://www.testurl.com:8080/subpath/subsubpath?uid=123&gid=456"];
Here listed some frequently-used accessors and results:
[url scheme]
http
[url host]
www.testurl.com
[url port]
8080
[url path]
/subpath/subsubpath
[url lastPathComponent]
subsubpath
[url query]
uid=123&gid=456
- NSURL类
- [IOS] 类 NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- NSURL
- nsurl
- NSURL
- NSURL
- NSURL
- Objective-C类,NSUrl介绍和使用
- [ios] NSURL
- java实现的简单词法分析器
- 关于org.hibernate.exception.SQLGrammarException: could not insert:
- scala lift环境搭建(9)
- 动画那点事 UIKit, CoreGraphics
- 关于在 KEIL C51 中嵌入汇编以及C51与A51间的相互调用
- NSURL类
- XML概念与解析库
- Wdp项目总结一-----该慢的时候要慢
- 小型机和PC服务器差异分析
- 多比图形控件开发指南(二)
- 修改线程的属性
- 改变键盘的颜色
- IE浏览器没有加载CSS文件的秘密
- 浅谈激活socket被阻塞的函数