2015-06-26 42 views
0

我試圖將Rotten Tomatoes API添加到我的應用,但它似乎並不喜歡它?我究竟做錯了什麼??錯誤說「不使用的格式字符串的數據論證」將爛Tomates API添加到iphone應用

-(void)main { 
    NSLog(@"Service has run"); 
    NSString *api_key = @"j4jz49tvf76cmnb4mwfyjvyt"; 
    NSString *search_term = [searchTerm stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSString *url = [NSString stringWithFormat:@"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=j4jz49tvf76cmnb4mwfyjvyt", api_key, search_term]; 

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url] 
               cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
           NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil]; 

           if (responseData !=nil) { 
            NSError *error =nil; 
            NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData 
                         options:kNilOptions error:&error]; 
            if (error) { 
             [delegate serviceFinished:self withError:YES]; 
            } else { 
             results = (NSArray *) [json valueForKey:@"movies"]; 
             [delegate serviceFinished:self withError:NO]; 
            } 
           } else { 
            [delegate serviceFinished:self withError:YES]; 
           } 
} 
@end 

回答

0

你的錯誤是在這條線

NSString *url = [NSString stringWithFormat:@"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=j4jz49tvf76cmnb4mwfyjvyt", api_key, search_term]; 

字符串格式能夠在一個字符串添加像%@數據參數作爲一個佔位符,然後在你的方法調用中用下面的參數替換它。

你想要做什麼大概是這樣的:

NSString *url = [NSString stringWithFormat:@"http://api.rottentomatoes.com/api/public/v1.0/lists/movies/box_office.json?limit=16&country=us&apikey=%@&search_term=%@", api_key, search_term]; 
+0

唉唉,感謝您的幫助,我的大學都提供的教程指導我寫它,我怎麼原本它。然而,即使在代碼更改之後,我的應用仍然不會從爛番茄中獲取電影列表。任何想法,爲什麼這是? –

+0

我沒有製作正確的URL,我只是提供了一個例子,說明它可能應該如何格式化。假設您對編程有基本的瞭解,我建議閱讀http://developer.rottentomatoes.com/docs開發人員文檔。 –

+0

我明白了。謝謝!! –

相關問題