2

我在我的程序中使用 JSONKit 来解析 google places api,但我的应用程序崩溃并出现以下错误 -[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);
4

1 回答 1

6

您设置的“ NSURLRequest”不正确,应该requestWithURL:改用。

代替

NSURLRequest *URLReq = [NSURL URLWithString:URL];

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

另外,一个快速的 FYI:Objective C 约定是对变量和 ivars 使用小写字母。使用大写字母作为您的班级名称。换句话说,将“ URLReq”更改为“ urlReq” ,将“”更改URL为“ url”(甚至更好,更具体的内容,例如“ googlePlaceURL”)。

于 2012-10-08T08:04:37.490 回答