我正在尝试使用HTTP请求ping eBay的API,但是Xcode给了我错误No known class method for selector stringWithFormat
。我想在这里做的是将searchField中输入的任何字符串追加到url的末尾。我究竟做错了什么?感谢任何帮助!
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if (sender != self.nextButton) return;
if (self.searchField.text.length > 0) {
self.responseData = [NSMutableData data];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL stringWithFormat:@"http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=***APP ID ****&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.12.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=3&itemFilter(2).paramName=Currency&itemFilter(2).paramValue=USD&itemFilter(3).name=ListingType&itemFilter(3).value=FixedPrice&keywords=%@", self.searchField.text]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
答案 0 :(得分:2)
您首先需要创建一个包含该网址的NSString
,然后在其中创建一个NSURL
。
NSString *urlString = [NSString stringWithFormat:@"http://example.com/",
self.searchField.text];
NSURL *url = [NSURL URLWithString:urlString];
答案 1 :(得分:0)
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://svcs.ebay.com/services/search/FindingService/v1?SECURITY-APPNAME=***APP ID ****&OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.12.0&RESPONSE-DATA-FORMAT=JSON&callback=_cb_findItemsByKeywords&REST-PAYLOAD&sortOrder=PricePlusShippingLowest&paginationInput.entriesPerPage=3&itemFilter(2).paramName=Currency&itemFilter(2).paramValue=USD&itemFilter(3).name=ListingType&itemFilter(3).value=FixedPrice&keywords=%@", self.searchField.text]]];
你错过了一个步骤,NSURL没有一个名为stringWithFormat的方法。