直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
2015-01-07 13:27
471 查看
今天在使用URLWithString拼接NSURL的时候出现了nil:
查了一下原因:
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尽量这样用:
这样就不容易出现URLWithString拼接NSURL为nil的情况,至少我这里就不会出现了。。
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尽量这样用:
// 这样用,就不容易出现像上面的问题 NSString *urlStr = [NSString stringWithFormat:@"%@%@?%@", host_url, baseurl, postURL]; urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url = [NSURL URLWithString:urlStr];
这样就不容易出现URLWithString拼接NSURL为nil的情况,至少我这里就不会出现了。。
相关文章推荐
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- 直接用URLWithString来拼接NSURL有时候得到的NSURL是为nil的
- NSURL使用urlWithString得到的对象为nil的问题原因以及解决
- [IOS] URLWithString拼接NSURL时,url=nil的问题
- [nsurl urlwithString:] 返回结果为nil
- NSURL URLWithString return nil 问题解决
- [iOS常见问题] 本地文件路径[NSURL URLWithString:] 获得的值为空nil
- [NSURL URLWithString:] 返回nil
- iOS:”NSURL中使用string拼接有时候会使NSURL成nil“解决方案
- IOS--错误集锦--reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
- NSURL fileURLWithPath 已知道是本地URL USURL urlWithString 不知道是本地还是网络,需要手动拼接 .主动获取MiMEType
- IOS--错误集锦--reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
- URLWithString:初始化为nil的问题
- [NSURL initFileURLWithPath:]: nil string parameter 错误的解决方案
- [NSURL initFileURLWithPath:]: nil string parameter 的问题
- [NSURL URLWithString:] returns nil
- 【NSURL URLWithString:url 】中string不为空而URL输出为空的情况
- [NSURL initFileURLWithPath:]: nil string parameter 错误的解决方案
- iOS --- 使用NSURL的urlWithString时,尽量先对字符串进行UTF8转码