美文网首页
model模型创建(模型里面套模型)

model模型创建(模型里面套模型)

作者: huicuihui | 来源:发表于2018-02-07 16:12 被阅读77次

    在做公司项目的时候, 数据是解析的json文件, json文件包括所有的数据(不同的列表数据和点进去的详情数据)。里面的内容样式是这样的:
    这是最外层的数据格式:


    image.png

    所以可以先创建一个model继承自NSObject
    里面有如下几个属性:

    #import <Foundation/Foundation.h>
    #import "ChildrenModel.h"
    @interface JournalModel : NSObject
    @property (nonatomic, copy)NSString *zhunze;
    @property (nonatomic, copy)NSString *zhunzecode;
    @property (nonatomic, strong)NSArray <ChildrenModel *>*children;
    
    @end
    

    .m的实现文件如下:

    #import "JournalModel.h"
    
    @implementation JournalModel
    - (void)setValue:(id)value forKey:(NSString *)key {
        //必须得加上这句话   不然除了下面重写的children有值,其它的字段会没数据
        [super setValue:value forKey:key];
    
        if ([key isEqualToString:@"children"]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *obj in value) {
                ChildrenModel *model = [[ChildrenModel alloc]init];
                [model setValuesForKeysWithDictionary:obj];
                [tempArray addObject:model];
            }
            self.children = tempArray;
        }
    }
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
    }
    @end
    

    注:

    只要是重写了- (void)setValue:(id)value forKey:(NSString *)key方法,必须得先调用父类的方法[super setValue:value forKey:key];,否则就会只显示重新赋值的key的值,其他的key会没有值。
    第二层的数据参数如下图:

    image.png
    创建一个model继承自NSObject
    里面有如下几个属性:
    #import <Foundation/Foundation.h>
    #import "ChildModel.h"
    @interface ChildrenModel : NSObject
    @property (nonatomic, copy)NSString *leibie;
    @property (nonatomic, copy)NSString *leibiecode;
    @property (nonatomic, copy)NSString *href;
    @property (nonatomic, strong)NSArray <ChildModel *>*child;
    
    @end
    

    .m实现文件如下:

    #import "ChildrenModel.h"
    
    @implementation ChildrenModel
    - (void)setValue:(id)value forKey:(NSString *)key {
        //这句话要实现, 不然视图上不显示数据。
        [super setValue:value forKey:key];
    
        if ([key isEqualToString:@"child"]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *obj in value) {
                ChildModel *model = [[ChildModel alloc]init];
                [model setValuesForKeysWithDictionary:obj];
                [tempArray addObject:model];
            }
            self.child = tempArray;
        }
    }
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    

    最后一层的数据如下图:


    image.png

    创建一个model继承自NSObject
    里面有如下几个属性:

    #import <Foundation/Foundation.h>
    #import "DetailModel.h"
    @interface ChildModel : NSObject
    @property (nonatomic, copy)NSString *zilei;
    @property (nonatomic, copy)NSString *zileicode;
    @property (nonatomic, strong)NSArray <DetailModel *>*detail;
    
    @end
    

    .m的实现文件:

    #import "ChildModel.h"
    
    @implementation ChildModel
    - (void)setValue:(id)value forKey:(NSString *)key {
        //这句话要实现, 不然视图上不显示数据。
        [super setValue:value forKey:key];
    
        if ([key isEqualToString:@"detail"]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            if ([value isKindOfClass:[NSArray class]]) {
                if (!([value count] > 0)) {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                    return;
                }
            } else {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                return;
            }
            for (NSDictionary *obj in value) {
                DetailModel *model = [[DetailModel alloc]init];
                [model setValuesForKeysWithDictionary:obj];
                [tempArray addObject:model];
            }
            self.detail = tempArray;
        }
    }
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    

    最终的详情model


    image.png

    .h

    #import <Foundation/Foundation.h>
    
    @interface DetailModel : NSObject
    @property (nonatomic, copy)NSString *shumu;
    @property (nonatomic, strong)NSArray *jie;
    @property (nonatomic, strong)NSArray *dai;
    
    @end
    

    .m

    #import "DetailModel.h"
    
    @implementation DetailModel
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    
    已经写完了,但是运行之后,解析数据,会崩溃到ChildModel文件下面的方法中: image.png

    控制台中报如下错误:
    -[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x60400085dcd0
    查找问题知道原因是因为:
    如果你要是对一个数组进行forin操作,并且你的数组是null 可能就会报这个错
    被枚举对象是一个NSNull值。
    本质就是:

    for(id a in [NSNull null]){
    
    }
    

    解决方法:
    添加下面方法判断:

            if ([value isKindOfClass:[NSArray class]]) {
                if ([value count] > 0) {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                    //如果数组内容为空, 不做下面操作。
                    return;
                }
            } else {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                //如果不是数组类型的直接跳出, 不做下面操作。
                return;
            }
    

    ChildModel的实现为:

    #import "ChildModel.h"
    
    @implementation ChildModel
    
    - (void)setValue:(id)value forKey:(NSString *)key {
        //必须得调用父类的方法,不然详情的没有数据。
        [super setValue:value forKey:key];
        if ([key isEqualToString:@"detail"]) {
            NSMutableArray *tempArray = [NSMutableArray array];
            if ([value isKindOfClass:[NSArray class]]) {
                if (!([value count] > 0)) {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                    //如果数组内容为空, 不做下面操作。
                    return;
                }
            } else {
                    //这行也得加上,不然后面用到model.detail的时候会出错。
                    self.detail = [NSArray array];
    
                //如果不是数组类型的直接跳出, 不做下面操作。
                return;
            }
            for (NSDictionary *obj in value) {
                DetailModel *model = [[DetailModel alloc]init];
                [model setValuesForKeysWithDictionary:obj];
                [tempArray addObject:model];
            }
            self.detail = tempArray;
        }
    }
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    
    }
    @end
    

    在一个文件中创建model

    后台返回的数据格式如下,

    {
        code = 1000;
        data =     (
                    {
                awardDetails =             (
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180329172758cut.jpg?t=0.8026015642824169";
                        effectiveText = "\U957f\U671f\U6709\U6548";
                        expressNeeded = 1;
                        expriseDate = 0;
                        inventory = 1193;
                        isExpire = 0;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 390;
                        prodId = 900807;
                        prodName = "\U300a\U4f01\U4e1a\U4f1a\U8ba1\U5904\U7406\U4e0e\U7eb3\U7a0e\U7533\U62a5\U771f\U8d26\U5b9e\U64cd\U300b\U300a\U4f01\U4e1a\U4f1a\U8ba1\U5904\U7406\U4e0e\U7eb3\U7a0e\U7533\U62a5\U771f\U8d26\U5b9e\U64cd\U300b\U300a\U4f01\U4e1a\U4f1a\U8ba1\U5904\U7406\U4e0e\U7eb3\U7a0e\U7533\U62a5\U771f\U8d26\U5b9e\U64cd\U300b";
                    },
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180324170206cut.jpg";
                        effectiveText = "\U957f\U671f\U6709\U6548";
                        expressNeeded = 1;
                        expriseDate = 0;
                        inventory = 0;
                        isExpire = 0;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 450;
                        prodId = 900808;
                        prodName = "\U300a\U4e2d\U5c0f\U4f01\U4e1a\U7a0e\U52a1\U4e0e\U4f1a\U8ba1\U5b9e\U52a1\U300b";
                    },
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180324170310cut.jpg";
                        effectiveText = "\U957f\U671f\U6709\U6548";
                        expressNeeded = 1;
                        expriseDate = 253402185600000;
                        inventory = 4;
                        isExpire = 0;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 5566;
                        prodId = 900809;
                        prodName = "\U300a\U65bd\U5de5\U4f01\U4e1a\U4f1a\U8ba1\U6838\U7b97\U4e0e\U5904\U7406\U5b9e\U6218\U300b";
                    },
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180324170350cut.jpg";
                        effectiveText = "\U957f\U671f\U6709\U6548";
                        expressNeeded = 1;
                        expriseDate = 0;
                        inventory = 995;
                        isExpire = 0;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 20;
                        prodId = 900810;
                        prodName = "\U300a\U8d22\U52a1\U62a5\U544a\U5206\U6790\U300b";
                    },
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180324170445cut.jpg?t=0.29931295238424793";
                        effectiveText = "\U957f\U671f\U6709\U6548";
                        expressNeeded = 1;
                        expriseDate = 0;
                        inventory = 1;
                        isExpire = 0;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 550;
                        prodId = 900811;
                        prodName = "\U300a\U8d22\U52a1Excel\U8fbe\U4eba\U901f\U6210\U300b";
                    },
                                    {
                        category = 10001;
                        coverPic = "https://pic.ningmengyun.com/PicLibrary/AppProducts/NaNaNaN/NaNaNaNaNaNaN/20180324170930cut.jpg?t=0.48121949416046905";
                        effectiveText = "\U5df2\U8fc7\U671f";
                        expressNeeded = 1;
                        expriseDate = 1540009425000;
                        inventory = 0;
                        isExpire = 1;
                        isLimitInventory = 0;
                        orderLevel = 0;
                        price = 680;
                        prodId = 900812;
                        prodName = "\U300a\U589e\U503c\U7a0e\U7eb3\U7a0e\U5b9e\U52a1\U4e0e\U8282\U7a0e\U6280\U5de7\U300b";
                    }
                );
                category =             {
                    categoryId = 10001;
                    name = "\U4f1a\U8ba1\U5b9e\U64cd";
                    num = 6;
                    order = 1;
                };
            }
        );
        msg = "<null>";
        subCode = 0;
    }
    

    data是一个数组,数组里面对象是字典,字典中有两个键值对awardDetailscategoryawardDetails是一个数组,category是一个字典。
    整个模型里面包括两个模型awardDetailscategory,可以在模型文件中再创建两个子模型AwardDetailModelCategoryModel
    实现如下:
    CashPrizeModel.h

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface AwardDetailModel : NSObject
    @property (nonatomic, copy)NSString *category;
    @property (nonatomic, copy)NSString *coverPic;
    @property (nonatomic, copy)NSString *effectiveText;
    @property (nonatomic, copy)NSString *expressNeeded;
    @property (nonatomic, copy)NSString *expriseDate;
    @property (nonatomic, copy)NSString *inventory;
    @property (nonatomic, copy)NSString *isExpire;
    @property (nonatomic, copy)NSString *isLimitInventory;
    @property (nonatomic, copy)NSString *orderLevel;
    @property (nonatomic, copy)NSString *price;
    @property (nonatomic, copy)NSString *prodId;
    @property (nonatomic, copy)NSString *prodName;
    @end
    
    @interface CategoryModel : NSObject
    @property (nonatomic, copy)NSString *categoryId;
    @property (nonatomic, copy)NSString *name;
    @property (nonatomic, copy)NSString *num;
    @property (nonatomic, copy)NSString *order;
    @end
    
    @interface CashPrizeModel : NSObject
    @property (nonatomic, strong)NSArray <AwardDetailModel *>*awardDetailModels;
    @property (nonatomic, strong)CategoryModel *categoryModel;
    @end
    
    NS_ASSUME_NONNULL_END
    

    CashPrizeModel.m文件

    #import "CashPrizeModel.h"
    
    @implementation AwardDetailModel
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    
    @implementation CategoryModel
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    
    @implementation CashPrizeModel
    - (void)setValue:(id)value forKey:(NSString *)key {
        [super setValue:value forKey:key];
        if ([key isEqualToString:@"awardDetails"]) {
            NSMutableArray *tempArray = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary *dic in value) {
                AwardDetailModel *model = [[AwardDetailModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [tempArray addObject:model];
            }
            self.awardDetailModels = tempArray;
        } else if ([key isEqualToString:@"category"]) {
            self.categoryModel = [[CategoryModel alloc]init];
            [self.categoryModel setValuesForKeysWithDictionary:value];
        }
    }
    - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
        
    }
    @end
    

    相关文章

      网友评论

          本文标题:model模型创建(模型里面套模型)

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