2012-07-04 42 views

回答

0

initWithString方法只能接受正常的NSString,你傳遞一個格式化的NSString,看看下面的代碼:

NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]]; 

這可能有點令人困惑,你可以按如下方式分解:

NSString *urlString = [NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quotedNumber]; 

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

現在您的字符串已正確格式化,並且NSURL initWithString方法將起作用!

而且,只是所以它是你更清晰的未來,你可以利用Objective-C的點表示法語法當您設置quoteNumber字符串,如下所示:

NSString *quoteNumber = self.textBox.text; 

而且,你想要將此引用號碼作爲數字傳遞到urlString(如%d所示),請記住quotedNumber是NSString對象,並且在嘗試將其傳遞給stringWithFormat方法時會崩潰。您必須先將字符串轉換爲NSInteger或NSUInteger。

請參閱this SO question如何做到這一點(不要擔心它很容易)!

1

您需要格式化您的字符串。試試這個:

NSString *urlString = [NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%@", quoteNumber]; 
NSURL *url = [[NSURL alloc] initWithString:urlString]; 
+0

你說得對,應該是%@ – wackytacky99

1

我想您所想的NSString的stringWithFormat:的:

[NSURL URLWithString:[NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%@", quoteNumber]] 

另請注意格式說明符的變化%@,因爲它的NSString(不是int)的實例

0

問題是

[NSURL initWithString:] 

需要的NSString類型的一個參數,但你傳遞了兩個參數。

您需要傳遞一個NSString參數。更改您的代碼從

NSURL *url = [[NSURL alloc] initWithString:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]; 

NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://TestSite.com/virdirectory/Webservice1/Service1.asmx/GetQuote?number=%d", quoteNumber]]; 
相關問題