- [NSURL _CFURLRequest]:发送到实例的无法识别的选择器

sin*_*sin 2 json nsurlconnection ios jsonkit google-places-api

我在我的程序中使用JSONKit解析谷歌地方api但我的应用程序崩溃时出现以下错误 - [NSURL _CFURLRequest]:无法识别的选择器发送到实例

     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);
Run Code Online (Sandbox Code Playgroud)

Mic*_*ann 6

NSURLRequest正确地设置了你的" "而应该使用requestWithURL:.

代替

NSURLRequest *URLReq = [NSURL URLWithString:URL];
Run Code Online (Sandbox Code Playgroud)

NSURLRequest * urlReq = [NSURLRequest requestWithURL: [NSURL URLWithString: URL]];
Run Code Online (Sandbox Code Playgroud)

另外,快速FYI:Objective C约定是对变量和ivars使用小写字母.使用大写字母表示您的班级名称.换句话说,将" URLReq" 更改为" urlReq"和" URL"更改为" url"(或甚至更好,更像" googlePlaceURL"的更具体的内容).