Objc:使用不符合RFC 2396的String创建NSURL

Joz*_*oze 3 iphone formatting objective-c nsurl nsstring

我有这种情况,我需要使用包含"."的字符串创建一个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. 
Run Code Online (Sandbox Code Playgroud)

所以问题是如何让我的URL不返回nil?

部分{"name":"My Name"}应该是JSON,但这似乎比使用NSDictionary然后使用JSON的解析器等更简单.

非常感谢你的帮助!

Max*_*Max 5

使用stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding转换你的字符串