NSURL返回Nil值 [英] NSURL returns Nil Value

查看:83
本文介绍了NSURL返回Nil值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码

NSString *string    = [NSString stringWithFormat:@" http://abc.com  /Demo/View.php?drinkId=%@&name=%@&comment=%@&date=%@&rating=%@&    ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];

NSURL *url          = [[NSURL alloc] initWithString:string];

在字符串中有值,但url返回nil. 任何人都可以说出发生这种情况的原因.

Here in string there is value but url returns nil. Can Anyone tell why this happened.

谢谢....

这行不通,所以这是我所做的"

"This won't work, so here's what I did instead"

NSString *string    = [NSString stringWithFormat:@"http://abc.com/Demo/View.php?drinkId=%@&name=%@&comment=%@&date=%@&rating=%@&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];

NSURL *url          = [[NSURL alloc] initWithString:string];

推荐答案

对于包含非法字符(例如空格)的URL,NSURL将返回nil.

NSURL will return nil for URLs that contain illegal chars, like spaces.

在将字符串与[NSURL URLWithString:]一起使用之前,请确保使用[NSString stringByAddingPercentEscapesUsingEncoding:]来转义所有不允许的字符.

Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].

这里是 NSString的类参考.

这篇关于NSURL返回Nil值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆