NSURL 提取参数字符串中某个键的单个值
我有一个 NSURL:
serverCall?x=a&y=b&z=c
获取 y 值的最快、最有效的方法是什么?
谢谢
I have an NSURL:
serverCall?x=a&y=b&z=c
What is the quickest and most efficient way to get the value of y?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
更新:
自 2010 年撰写本文以来,Apple 似乎已经为此目的发布了一套工具。请参阅下面的答案。
老派解决方案:
嗯,我知道你说“最快的方法”,但在我开始使用
NSScanner
进行测试后,我就是做不到停止。虽然这不是最短的方法,但如果您打算经常使用该功能,那么它肯定很方便。我创建了一个URLParser
类,它使用NSScanner
获取这些变量。使用很简单:执行此操作的类如下(*源文件位于帖子底部):
URLParser.h
URLParser.m
*if你不喜欢复制和粘贴,你可以下载源文件 - 我写了一篇关于这个的快速博客文章 此处。
UPDATE:
Since 2010 when this was written, it seems Apple has released a set of tools for that purpose. Please see the answers below for those.
Old-School Solution:
Well I know you said "the quickest way" but after I started doing a test with
NSScanner
I just couldn't stop. And while it is not the shortest way, it is sure handy if you are planning to use that feature a lot. I created aURLParser
class that gets these vars using anNSScanner
. The use is a simple as:And the class that does this is the following (*source files at the bottom of the post):
URLParser.h
URLParser.m
*if you don't like copying and pasting you can just download the source files - I made a quick blog post about this here.
这里有很多自定义 url 解析器,记住 NSURLComponents 是你的朋友!
提取了 url 编码参数
这是一个示例,我为“page” Swift
Objective-C
“为什么要重新发明轮子!” - 聪明人
So many custom url parsers here, remember NSURLComponents is your friend!
Here is an example where I pull out a url encoded parameter for "page"
Swift
Objective-C
"Why reinvent the wheel!" - Someone Smart
我很确定你必须自己解析它。不过,情况还不错:
I'm pretty sure you have to parse it yourself. However, it's not too bad:
在 Swift 中,您可以使用 NSURLComponents 将 NSURL 的查询字符串解析为 [AnyObject]。
然后,您可以从中创建一个字典(或直接访问项目)以获取键/值对。作为一个例子,我用它来解析 NSURL 变量 url:
In Swift you can use NSURLComponents to parse the query string of an NSURL into an [AnyObject].
You can then create a dictionary from it (or access the items directly) to get at the key/value pairs. As an example this is what I am using to parse a NSURL variable url:
我一直在使用此类别:https://github.com/carlj/NSURL-Parameters。
它体积小且易于使用:
I've been using this Category: https://github.com/carlj/NSURL-Parameters.
It's small and easy to use:
您可以使用 Mac 版 Google 工具箱。
它向 NSString 添加了一个函数,用于将查询字符串转换为字典。
http://code.google.com/p/google-toolbox-for- mac/
它就像一个魅力
You can use Google Toolbox for Mac.
It adds a function to NSString to convert query string to a dictionary.
http://code.google.com/p/google-toolbox-for-mac/
It works like a charm
下面是一个 Swift 2.0 扩展,它提供了对参数的简单访问:
示例用法:
Here's a Swift 2.0 extension that provides simple access to parameters:
Sample usage:
我编写了一个简单的类别来扩展 NSString/NSURL,它允许您单独提取 URL 查询参数或作为键/值对的字典:
https://github.com/nicklockwood/RequestUtils
I wrote a simple category to extend NSString/NSURL that lets you extract URL query parameters individually or as a dictionary of key/value pairs:
https://github.com/nicklockwood/RequestUtils
我使用基于 @Dimitris 解决方案的类别方法来做到这一点
I did it using a category method based on @Dimitris solution
当前的所有答案都是特定于版本的或不必要的浪费。如果您只想要一个值,为什么要创建字典呢?
这是一个支持所有 iOS 版本的简单答案:
All of the current answers are version specific or needlessly wasteful. Why create a dictionary if you only want one value?
Here's a simple answer that supports all iOS versions:
你可以简单地做到这一点:
它返回像这样的字典:Key = value
You can do that easy :
It return Dictionary like it : Key = value
我稍微编辑了 Dimitris 的代码,以实现更好的内存管理和效率。此外,它也适用于 ARC。
URLParser.h
URLParser.m
I edited Dimitris' code slightly for better memory management and efficiency. Also, it works in ARC.
URLParser.h
URLParser.m
最快的是:
Quickest is: