iOS网络编程:四、Block方式实现异步

来源:互联网 发布:sift特征python 编辑:程序博客网 时间:2024/06/04 17:34
    //1创建URL    NSURL *url = [NSURL URLWithString:CQGETUEL];    //2创建请求对象    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];    //3 发送异步请求  --(异步请求不是多线程,因为都在 mainQueue 中,没有散分支)    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];        NSArray *arr = dic[@"news"];        self.data = [NSMutableArray array];        for (NSDictionary *dic in arr) {            News *news = [[News alloc] init];            [news setValuesForKeysWithDictionary:dic];            [self.data addObject:news];        }     }];
0 0