2013-01-17 18 views
0

我使用的堆棧溢出的問題,此代碼正常工作:URLWithString: returns nil數據的說法不使用的格式字符串,但

//localisationName is a arbitrary string here 
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName]; 
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL* url = [NSURL URLWithString:webStringURL]; 

當我複製到我的代碼,有沒有任何問題,但是當我修改它使用我的網址,我得到了這個問題:

數據參數不使用格式字符串。

但它工作正常。在我的項目:

.H:

NSString *localisationName; 

.M:

NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName]; 
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL* url = [NSURL URLWithString:webStringURL]; 

[_webView loadRequest:[NSURLRequest requestWithURL:url]]; 

我怎樣才能解決這個問題?從我的代碼中遺漏了什麼?

回答

1

原始字符串中的@用作插入值webName的佔位符。在你的代碼中,你沒有這樣的佔位符,所以你告訴它把webName放入你的字符串中,但你不是在哪裏。

如果你不想在字符串中插入webName,那麼你的代碼的一半是多餘的。所有你需要的是:

NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere"; 
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL* url = [NSURL URLWithString:webStringURL]; 

[_webView loadRequest:[NSURLRequest requestWithURL:url]]; 
+0

謝謝你,它工作正常! –

0

+stringWithFormat:方法return a string created by using a given format string as a template into which the remaining argument values are substituted。在第一個代碼塊中,%@將被替換爲值webName

在修改後的版本中,格式參數,這是@"http://en.wikipedia.org/wiki/Hősök_tere",不含任何format specifiers,所以

NSString* stringURL = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Hősök_tere", webName];

只是運行像這(與警告Data argument not used by format string.

NSString* stringURL = @"http://en.wikipedia.org/wiki/Hősök_tere";

相關問題