当我使用sendAsynchronousRequest
方法的时候这个方法内块会在执行外部代码后执行?sendAsynchronousRequest方法会在块外代码前执行块代码
__block NSDictionary *dict;
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
NSInteger httpStatus = [((NSHTTPURLResponse *)response) statusCode];
NSLog(@"httpStatus inside block:%d",httpStatus);
if ([data length]>0 && connectionError==nil)
{
dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
}
else if (connectionError)
{
UIAlertView *alt=[[UIAlertView alloc] initWithTitle:@"Error" message:[connectionError localizedDescription] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alt show];
}
}];
return dict;//it will return null because it will run before execute inner block of sendAsynchronousRequest
添加完毕块来回报您的词典内容 – nanjunda
是。这就是使该方法异步的原因。直到请求完成后才会调用“completionHandler”。这似乎是一个不完整的问题,也许你可以澄清你在问什么? – Jonah