我对
NSUrlSession
和 API 非常困惑。这是我第一次尝试使用 API,所以请以最简单的形式解释这一点。
我找到了一个获取天气的 API,我为天气位置创建了一个字符串,然后执行了所有 NSUrl / nsurl 请求。我的目标是输出所有内容,以便我可以看到该 API 的密钥。这是我到目前为止所拥有的,但它显示的只是:
“程序以退出代码 0 结束”
我真的不知道
NSUrlsession
期间发生了什么,因为我通过 YouTube 视频学习了如何将 API 与 NSUrlConnection
结合使用。
NSString *location = @"London";
NSString *weatherString = [NSString stringWithFormat:@"https://api.openweathermap.org/data/2.5/weather?q=%@", location];
NSURL *weatherURL = [NSURL URLWithString:weatherString];
NSURLRequest *request = [NSURLRequest requestWithURL:weatherURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary *weatherDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
NSLog(@"%@", [weatherDictionary description]);
}];
很难从这段代码中看出,但以下一个或多个问题可能会导致您的问题: