Objective-C:如何向 NSURL 添加查询参数?
假设我有一个 NSURL
?无论是否已有空查询字符串,如何向 NSURL
的 query
添加一个或多个参数?即,有谁知道这个功能的实现吗?
- (NSURL *)URLByAppendingQueryString:(NSString *)queryString
这样它就满足这个 NSURL+AdditionsSpec.h
文件:
#import "NSURL+Additions.h"
#import "Kiwi.h"
SPEC_BEGIN(NSURL_AdditionsSpec)
describe(@"NSURL+Additions", ^{
__block NSURL *aURL;
beforeEach(^{
aURL = [[NSURL alloc] initWithString:@"http://www.example.com"];
aURLWithQuery = [[NSURL alloc] initWithString:@"http://www.example.com?key=value"];
});
afterEach(^{
[aURL release];
[aURLWithQuery release];
});
describe(@"-URLByAppendingQueryString:", ^{
it(@"adds to plain URL", ^{
[[[[aURL URLByAppendingQueryString:@"key=value&key2=value2"] query] should]
equal:@"key=value&key2=value2"];
});
it(@"appends to the existing query sting", ^{
[[[[aURLWithQuery URLByAppendingQueryString:@"key2=value2&key3=value3"] query] should]
equal:@"key=value&key2=value2&key3=value3"];
});
});
});
SPEC_END
Let's say I have an NSURL
? Whether or not it already has an empty query string, how do I add one or more parameters to the query
of the NSURL
? I.e., does anyone know of an implementation of this function?
- (NSURL *)URLByAppendingQueryString:(NSString *)queryString
So that it satisfies this NSURL+AdditionsSpec.h
file:
#import "NSURL+Additions.h"
#import "Kiwi.h"
SPEC_BEGIN(NSURL_AdditionsSpec)
describe(@"NSURL+Additions", ^{
__block NSURL *aURL;
beforeEach(^{
aURL = [[NSURL alloc] initWithString:@"http://www.example.com"];
aURLWithQuery = [[NSURL alloc] initWithString:@"http://www.example.com?key=value"];
});
afterEach(^{
[aURL release];
[aURLWithQuery release];
});
describe(@"-URLByAppendingQueryString:", ^{
it(@"adds to plain URL", ^{
[[[[aURL URLByAppendingQueryString:@"key=value&key2=value2"] query] should]
equal:@"key=value&key2=value2"];
});
it(@"appends to the existing query sting", ^{
[[[[aURLWithQuery URLByAppendingQueryString:@"key2=value2&key3=value3"] query] should]
equal:@"key=value&key2=value2&key3=value3"];
});
});
});
SPEC_END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
从iOS 7开始,您可以使用非常简单的NSURLComponents。看一下这些示例:
示例 1
示例 2
示例 3
但是您可以使用 NSURLComponents 执行许多其他操作,请查看 Apple 文档或此链接上的 NSURLComponents 类参考: http://nshipster.com/nsurl/
Since iOS 7 you can use NSURLComponents that is very simple to use. Take a look on these examples:
Example 1
Example 2
Example 3
But you can do many other things with NSURLComponents take a look on NSURLComponents class reference on Apple documentation or on this link: http://nshipster.com/nsurl/
这是通过您的规范的实现:
这是
NSString
的实现:Here's an implementation that passes your specs:
And here is an implementation for
NSString
:iOS8+ 现代方式
添加(或替换“ref”值(如果存在))ref=impm 到 min60.com 上的 url
The iOS8+ modern way
adding (or replacing 'ref' value if exists) ref=impm to url which is on min60.com
对于那些不想在使用
NSURLComponents
构建NSURL
时编写样板代码的人来说,这只是一个友好的帖子。从 iOS8 开始,我们有了
NSURLQueryItem
来帮助快速构建 URL 请求。我写了一个方便的小类别来简化工作,您可以在这里获取:URLQueryBuilder
以下示例说明了使用它是多么容易:
Just a friendly post for those who don't want to write boilerplate code while building
NSURL
withNSURLComponents
.Since iOS8 we have
NSURLQueryItem
that helps building URL request freaking fast.I wrote a little handy category to ease the work, that you can grab here: URLQueryBuilder
Here is example of how easy it is to work with it:
我有一个
NSURLComponents
扩展,可以快速添加查询项:要使用,
I have an extension to
NSURLComponents
that add query item, in swift:To use,
如果您使用 RestKit,它会提供对 NSString 的添加。其中之一是:
所以你可以这样做:
If you're using RestKit it provides additions to NSString. One of which is:
So you could do:
正如其他人提到的,您可以使用 NSURLComponents 来构造 URL。
As others have mentioned, you can use
NSURLComponents
to construct URLs.NSURL 是不可变的,因此您无法直接基于 NSURL 实现此功能。相反,您必须获取 URL 的字符串表示形式,将参数附加到该字符串,然后创建一个新的 NSURL。
这听起来不是一个好的解决方案。除非有充分的理由,否则最好在最后一刻才使用字符串,并且仅在收到完全形成的请求时才创建 NSURL。
NSURL is not mutable so you cannot implement this functionality directly based on NSURL. Instead you will have to obtain the string representation of the URL, append your parameters to that and then create a new NSURL.
This does not sound like a good solution. Unless there is a good reason, it is better to work with strings until the last moment and only create an NSURL when you have your fully formed request.