iphone - NSURL 和 URL 有效性

标签 iphone objective-c ios nsurl

我正在使用来自 API 的 URL,其中搜索词用引号括起来。例如: http://www.example.com/searchterm="搜索"

但是,由于引号,我的 NSURL(由 URLWithString 生成)由于 URL 无效而为 nil。有没有解决的办法?谢谢!

最佳答案

可以在引号前加反斜杠,或者URL编码值%22:

http://www.example.com/searchterm=\"search\"

http://www.example.com/searchterm=%22search%22

或者您可以在将其用作 NSURL 之前删除引号。

NSString * searchUrl = @"http://www.example.com/searchterm=\"search\"";
searchUrl = 
  [searchUrl stringByReplacingOccurrencesOfString:@"\"" withString:@""]; // something to this effect, not tested.

然后你就可以开始正常的 NSURL 调用了:

[NSURL URLWithString: searchUrl];

关于iphone - NSURL 和 URL 有效性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7426048/

相关文章:

ios - AFNetworking 2.0.0-RC2 : No visible @interface for 'DNHackerNewsAPIClient' declares the selector 'getPath:parameters:success:failure:' ?

objective-c - 我怎样才能只在文件的一部分而不是整个文件中禁用 ARC?

objective-c - 我在快速尝试同等的 Objective-C block 时遇到了麻烦

ios - 如何仅在单击搜索栏时显示表格 View ?

ios - CADisplayLink 导致 touchesBegan 和 touchesEnded 之间存在延迟?

iphone - 当应用程序进入和退出后台时暂停和恢复核心动画

iphone - 将现有的 C 代码移植到 iOS/iPhone

iphone - iOS UIActivity View Controller : Add To Reading List Button?

ios - 在 applicationWillResignActive : is called 后,Sprite Kit 中运行音频文件的操作停止运行

ios - 执行按钮操作后 UICollectionView rollToItemAtIndex