直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
2017-01-09 20:57
411 查看
今天在使用URLWithString拼接NSURL的时候出现了nil:
[objc] view
plain copy
print?
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?%@",host_url,baseurl,postURL]];
查了一下原因:
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
大概的意思是说,转义方面的,但并非我这里拼接为nil的原因。不过他给的解决方案缺让我最终拼接的NSURL不为nil了。。
所以以后在使用URLWithString来拼接NSURL尽量这样用:
[objc] view
plain copy
print?
// 这样用,就不容易出现像上面的问题
NSString *urlStr = [NSString stringWithFormat:@"%@%@?%@", host_url, baseurl, postURL];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
这样就不容易出现URLWithString拼接NSURL为nil的情况,至少我这里就不会出现了。。
转载自:http://blog.csdn.net/zengraoli/article/details/16878393
[objc] view
plain copy
print?
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?%@",host_url,baseurl,postURL]];
查了一下原因:
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
大概的意思是说,转义方面的,但并非我这里拼接为nil的原因。不过他给的解决方案缺让我最终拼接的NSURL不为nil了。。
所以以后在使用URLWithString来拼接NSURL尽量这样用:
[objc] view
plain copy
print?
// 这样用,就不容易出现像上面的问题
NSString *urlStr = [NSString stringWithFormat:@"%@%@?%@", host_url, baseurl, postURL];
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
这样就不容易出现URLWithString拼接NSURL为nil的情况,至少我这里就不会出现了。。
转载自:http://blog.csdn.net/zengraoli/article/details/16878393
相关文章推荐
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- [IOS] URLWithString拼接NSURL时,url=nil的问题
- NSURL使用urlWithString得到的对象为nil的问题原因以及解决
- [iOS常见问题] 本地文件路径[NSURL URLWithString:] 获得的值为空nil
- [NSURL URLWithString:] 返回nil
- iOS:”NSURL中使用string拼接有时候会使NSURL成nil“解决方案
- NSURL URLWithString return nil 问题解决
- [nsurl urlwithString:] 返回结果为nil
- [NSURL initFileURLWithPath:]: nil string parameter 错误的解决方案
- [iphone][xcode]NSURL urlwithstring
- 使用MPMediaPlayerController播放视频提示[NSURL initFileURLWithPath:]: nil string parameter错误的解决办法
- reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 产生崩溃
- [iphone][xcode]NSURL urlwithstring
- iOS --- 使用NSURL的urlWithString时,尽量先对字符串进行UTF8转码
- iOS URLWithString返回为空nil的解决方案
- stringWithContentsOfURL:encoding:error方法返回nil
- IOS--错误集锦--reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
- IOS--错误集锦--reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'