2011-08-31 96 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];