我希望使用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的初学者。
答案 0 :(得分:1)
您可以处理回复:
- (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];
}
答案 1 :(得分:1)
这是您的回复代码。
- (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];
}