我使用塊從一個類的響應中獲取頭字段,並且我必須在另一個類中獲取它。如何立即獲得塊結果?
我實現這樣的
在第一類代碼:
- (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
是的,這是一個錯字錯誤,對不起。 –