zoukankan      html  css  js  c++  java
  • JsonModel的使用(ios程序员必须熟练掌握,太激动了)

    demo下载:https://github.com/MartinLi841538513/JsonModelDemo2

    简单说,就是dictionary to model.  专业一些就是:解析json数据。

    可以为您省下大量的代码,时间,精力,还能提高准确度。

    操作步骤:

    一,引入JsonModel :这里我使用了pods, pod 'JSONModel'

    二,对应url建立model,如:http://semantictec.com/tec/api/mm/category?source=sears&agent=123&client=ios

    这里必然需要建立2个model:一个是category,一个是self。

    这里我贴代码进来吧,大家注意看:

    Cat.h

    注意:1,继承JSONModel

         2,创建Cat协议

            3,实现所有或者部分对应属性(命名必须和Json属性名一模一样)

    #import "JSONModel.h"
    
    @protocol Cat @end
    
    @interface Cat : JSONModel
    @property (strong, nonatomic) NSString* id;
    @property (strong, nonatomic) NSString* name;
    @property (assign, nonatomic) long products;
    @end

    UserModel.h

    注意:1,继承JSONModel

         2,实现所有或者部分对应属性

         3,对应到categories属性的地方,做如下处理

    #import <JSONModel/JSONModel.h>
    #import "Cat.h"
    @interface UserModel : JSONModel
    @property (strong, nonatomic) NSString* source;
    @property (strong, nonatomic) NSString* agent;
    @property (strong, nonatomic) NSString* client;
    @property (strong, nonatomic) NSArray<Cat>* categories;
    @end

    三,开始发送请求获取数据了

    #import "ViewController.h"
    #import "UserModel.h"
    #import "JSONModelLib.h"
    @interface ViewController ()
    {
        UserModel *userModel;
    }
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        userModel = [[UserModel alloc] initFromURLWithString:@"http://semantictec.com/tec/api/mm/category?source=sears&agent=123&client=ios" completion:^(UserModel *model, JSONModelError *err) {
            
                NSLog(@"loans: %@",userModel);
                NSLog(@"%@",model);
        }];
    }

    1,import :XXXModel.h   JSONModelLib.h

    2,这里以initFromURLWithString:请求数据为例(还有很多其他的方法)。

    这里提供两种获取数据的方式:这里的userModel,model是一样的数据,你可以根据实际情况选择一种。

    在上面的例子中,你添加了两个jsonModel类,如果还有三级,四级分类,是不是要添加更多。这样无疑也是麻烦的,而且浪费了一些资源。

    其实,后来仔细想想,他们原理上是可以放在一个.h .m文件中的。后面我用实例证明了我的说法:

    .h

    #import <JSONModel/JSONModel.h>
    
    @protocol Cat @end
    
    @interface Cat : JSONModel
    @property (strong, nonatomic) NSString* id;
    @property (strong, nonatomic) NSString* name;
    @property (assign, nonatomic) long products;
    @end
    
    @interface UserModel : JSONModel
    @property (strong, nonatomic) NSString* source;
    @property (strong, nonatomic) NSString* agent;
    @property (strong, nonatomic) NSString* client;
    @property (strong, nonatomic) NSArray<Cat>* categories;
    @end

    .m

    #import "UserModel.h"
    
    @implementation Cat
    @end
    
    @implementation UserModel
    @end
  • 相关阅读:
    Linux 内核剖解(转)
    计算机系统的分层结构
    Linux学习之路三:重要概念之Linux系统层次结构
    库函数与系统调用的联系与区别
    库函数与系统调用
    库函数调用和系统调用的区别
    标准库函数和系统调用的区别
    关于Linux操作系统层次结构分析
    linux标准输入输出
    C语言的标准输入输出
  • 原文地址:https://www.cnblogs.com/MartinLi841538513/p/3936952.html
Copyright © 2011-2022 走看看