美文网首页
无标题文章

无标题文章

作者: 丁晓鹏 | 来源:发表于2017-10-12 18:30 被阅读0次

    - (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSURLSession *session = [NSURLSession sharedSession];

    NSURL *geturl = [NSURL URLWithString:URL];

    NSURLSessionTask *task = [session dataTaskWithURL:geturl completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

    NSString *resultStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"resultStr -> %@",resultStr);

    }];

    [task resume];

    }

    - (void)json
    {
        _dic = [[NSMutableDictionary alloc]init];
        NSURL *url = [NSURL URLWithString:URL];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            
            _dic =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            _dataArr = [[NSArray alloc]init];
            _dataArr = [[_dic objectForKey:@"result"]objectForKey:@"data"];
            
            _modelArr = [[NSMutableArray alloc]init];

            
            for (int i = 0; i<_dataArr.count; i++) {
                Model *_model = [[Model alloc]init];
                _model.title = [_dataArr[i] objectForKey:@"title"];
                _model.albums = [_dataArr[i] objectForKey:@"albums"][0];
                [_modelArr addObject:_model];
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
               
                [_collectionView reloadData];
            });
        }];

        [task resume];
    }

    相关文章

      网友评论

          本文标题:无标题文章

          本文链接:https://www.haomeiwen.com/subject/nnpoyxtx.html