我正在使用来自 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/