i am using JSONKit in my program to parse google places api but my app crashes with the following error -[NSURL _CFURLRequest]: unrecognized selector sent to instance

     NSString* URL = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=28.632808,77.218276&radius=500&types=atm&sensor=false&key=AIzaSyDHft2g5IDshIpXS17uOtZzkqGGgj-p1RQ"];

NSError* error = nil;
NSURLResponse* response = nil;


NSURLRequest *URLReq = [NSURL URLWithString:URL];
//[request setURL:URL];
//[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
//[request setTimeoutInterval:30];

NSData* data = [NSURLConnection sendSynchronousRequest:URLReq returningResponse:&response error:&error];

if (error)
{
    NSLog(@"Error performing request %@", URL);
    NSLog(@"%@", [error localizedDescription]);
    return;
}

NSDictionary *json = [data objectFromJSONData];

NSArray *places = [json objectForKey:@"results"];


NSLog(@"Google Data: %@", places);
有帮助吗?

解决方案

You're setting up your "NSURLRequest" incorrectly and should be using requestWithURL: instead.

Instead of

NSURLRequest *URLReq = [NSURL URLWithString:URL];

do

NSURLRequest * urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: URL]];

Also, a quick FYI: Objective C convention is to use lowercase letters for variables and ivars. Use capital letters for your class names. In other words, change "URLReq" to "urlReq" and "URL" to "url" (or even better than that, something more specific like "googlePlaceURL").

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top