Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用NSLog显示NSURLSession的输出?_Ios_Objective C_Nsurlsession_Nslog - Fatal编程技术网

Ios 如何使用NSLog显示NSURLSession的输出?

Ios 如何使用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

我希望使用NSLog显示URl的输出

ViewController.h

@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块之外的响应?