ブロックを使用して、あるクラスの応答からヘッダー フィールドを取得していますが、それを別のクラスで取得する必要があります。
このようなコードを実装しました
ファーストクラスで:
- (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
nullとして取得しているファーストクラスにあります。
クラスでもuserAuthentication
私はnullになっています。
しかし、しばらくすると、コールバック メソッドが呼び出され、応答が正しく表示されcompletionHandler
ます。
では、firstClass で応答を取得するにはどうすればよいでしょうか。