in Blog Posts, Solution

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

Leave a Reply for Haoxiang Li Cancel Reply

Write a Comment

Comment

  1. In the latest thing cannabis growers are spoiled for the duration of choice – https://trilogeneseeds.com/collections/feminized-seeds . There are more strains on the trade in than period in the over and done with, and numerous growers procure entranced a refinement to the original and improved selection of autoflowers. Autoflowering genetics be imbued a everything to offer. There are varieties high in CBD, blaring in THC, and those with a well-disposed level of both. That’s not to allusion the imagine inconsistency in terpene profiles. But it’s not all sunshine and roses. Autoflower strains cause a play the host of advantages, but they also house disadvantages that turn some growers off.

  2. CBD, or cannabidiol, has been a game changer due to the fact that me. I’ve struggled with anxiety in search years and have tried diverse various medications, but nothing has worked as fount as CBD. It helps me to feel sang-froid and relaxed without any side effects. I also espy that it helps with take and labour management. I’ve tried a variety of brands, but I’ve bring about that the ones that are lab tested and take a good position are the most effective. Comprehensive, I highly recommend CBD for anyone who struggles with worry, be in the land of nod issues, or lasting pain.