YYModel第三方框架

来源:互联网 发布:入住淘宝达人 编辑:程序博客网 时间:2024/06/08 11:06
#import "YYModel.h"

使用YYModel把字典数组转模型数组的核心代码

  • 参数1:要转成的模型类型
  • 参数2:字典数组
  • 结果:模型数组
self.modelArray = [NSArray yy_modelArrayWithClass:[NewsModel class] json:dictArr];

使用YYModel把字典数组转模型数组的完整逻辑

- (void)loadData {    // URL    NSURL *url = [NSURL URLWithString:@"http://news.coolban.com/Api/Index/news_list/app/2/cat/0/limit/20/time/1457168894/type/0?channel=appstore&uuid=19C2BF6A-94F8-4503-8394-2DCD07C36A8F&net=5&model=iPhone&ver=1.0.5"];    // 发起任务获取JSON数据    [[[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        // 反序列化JSON == 字典数组        NSArray *dictArr = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];        // YYModel实现字典数组转模型数组        self.modelArray = [NSArray yy_modelArrayWithClass:[NewsModel class] json:dictArr];        // 回到主线程刷新UI        [[NSOperationQueue mainQueue] addOperationWithBlock:^{            // [self.tableView reloadData];        }];    }] resume];}
原创粉丝点击