2011-08-31 99 views
8

这下面是我的代码NSURL返回空值

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返回零。 任何人都可以告诉为什么发生这种情况。

感谢....

“这是行不通的,所以这里是我做了什么,而不是”

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]; 
+0

高兴的是,我的回答帮助。你能帮助大家解决这个问题吗? –

+0

你会告诉我锄头来回答这个问题吗? – Vijay

+0

您单击答案旁边的灰色复选标记,使其变为绿色。 –

回答

26

NSURL将返回nil对于含有非法字符,如空格的URL。

在使用字符串[NSURL URLWithString:]之前,请确保使用[NSString stringByAddingPercentEscapesUsingEncoding:]来逃避所有禁止使用的字符。

这里是the class reference for NSString

+0

感谢Mike Hay Now问题已解决.. – Vijay

+0

有空格。 – Nekto

+0

您向我们展示的代码有空格。 – 0x8badf00d

-1

大家好现在我的问题在这里解决了我在“http”之前留下了一个空白空间。

正确的代码

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

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