Objc:使用不符合 RFC 2396 的字符串创建 NSURL
我遇到这种情况,我需要创建一个包含 " 的字符串的 URL。
例如:
NSString *myStringWithURL = [NSString stringWithFormat:
@"http://myurl.com/setParam.php?setName=
{\"name\":\"%@\"} ", name];
// myStringWithURL will return the desired URL but it is a String.
// of course I need it as URL in order to make the request.
NSURL *targetURL = [NSURL URLWithString : myStringWithURL];
//This will return nil.
所以问题是如何让我的 URL 不返回 nil?
{"name":"My Name"} 部分应该是JSON 但这似乎比使用 NSDictionary 然后使用 JSON 解析器等更简单,
非常感谢您的帮助!
I have this situation where I need to make a URL with a string that contains ".
For instance:
NSString *myStringWithURL = [NSString stringWithFormat:
@"http://myurl.com/setParam.php?setName=
{\"name\":\"%@\"} ", name];
// myStringWithURL will return the desired URL but it is a String.
// of course I need it as URL in order to make the request.
NSURL *targetURL = [NSURL URLWithString : myStringWithURL];
//This will return nil.
So the question is how to get my URL to not return nil?
The part {"name":"My Name"} should be a JSON but this seemed simpler than using a NSDictionary and then using the parser of JSON and so on.
I thank you very much for your help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding 转换字符串
use stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding to convert your string