我使用块从一个类的响应中获取头字段,并且我必须在另一个类中获取它。如何立即获得块结果?
我实现这样的
在第一类代码:
- (void)viewDidLoad {
[super viewDidLoad];
UserAuthentication *auth = [[UserAuthentication alloc]init];
NSDictionary *dict = [auth getUserConfiguration];
NSLog(@"%@",dict);
}
在userAuthentication类:
-(NSDictionary *)getUserConfiguration;
{
__block NSDictionary *resultDictionary;
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:@"http://72.52.65.142:8083/auth"]
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
resultDictionary = [httpResponse allHeaderFields];
NSLog(@"%@",resultDictionary);
}
}] resume];
NSLog(@"%@",resultDictionary);
return resultDictionary;
}
这里我的问题是头等我越来越dict
为空。
即使在userAuthentication
类也是我变空。
但经过一段时间回调方法正在调用,然后我可以在completionHandler
正确看到响应。
那么我如何才能在firstClass中获得响应?
得到这个错误:函数'完成'的隐式声明在C99中是无效的 – Himanth
所以我只好改变完成操作完成操作 – Himanth
是的,这是一个错字错误,对不起。 –