當我使用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