Ios 如何使用NSLog显示NSURLSession的输出?
我希望使用NSLog显示URl的输出 ViewController.hIos 如何使用NSLog显示NSURLSession的输出?,ios,objective-c,nsurlsession,nslog,Ios,Objective C,Nsurlsession,Nslog,我希望使用NSLog显示URl的输出 ViewController.h @interface ViewController : UIViewController{ NSURLSession *session; NSString *londonWeatherUrl; NSURLRequest *request; } ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup afte
@interface ViewController : UIViewController{
NSURLSession *session;
NSString *londonWeatherUrl;
NSURLRequest *request;
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";
request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {}] resume];
session = [NSURLSession sharedSession];
NSLog(@"%@",); // what goes here ?
}
有人能告诉我在NSLog中需要什么,以便该URL的输出显示在日志中吗。
对不起,我是目标C的初学者。您可以处理响应呼叫:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";
request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
// handle response
NSLog(@"%@", response);
}] resume];
}
这是您的回复代码
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
londonWeatherUrl = @"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0";
request = [NSURLRequest requestWithURL:
[NSURL URLWithString:londonWeatherUrl]];
[[session dataTaskWithURL:[NSURL URLWithString:londonWeatherUrl]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSError* error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; //Provide your NSData here to convert to dictionary
//NSArray *responses = [json objectForKey:@"YOUR KEY"]; //Provide your key if contain array or else directly use as dictionary
NSLog(@"Data dictionary: %@", jsonDictionary);
NSLog(@"NSURLResponse: %@", response);
}] resume];
session = [NSURLSession sharedSession];
}
“Url的输出”-你的意思是“响应”@Sandr-是的,响应
NSLog
将转到该完成处理程序(介于{
和}
之间)。您的数据
是完成处理程序的一个参数。您可以以十六进制格式显示该数据,或者必须首先解析它(JSON、UIImage等)。如何访问completionHandler块之外的响应?