YYModel使用詳解

根據需求、後臺返回以下數據、須要解析爲model類、方便操做數據。json

NSDictionary *returnDic = @{
                                @"birthPlace":@"寧夏固原市彭陽縣草廟鄉",
                                @"education":@"博士",
                                @"id":@"115",

                                //字典
                                @"personalMessage":@{
                                        @"marryAge":@"30",
                                        @"marryStatus":@"1",
                                        @"paddress":@"寧夏固原市",
                                        @"phone":@"158*****289",
                                        @"pname":@"sxk",
                                        },
                                @"babyFatherInfo" : @{
                                        @"bFatherAge" : @"38",
                                        @"bFatherUnit" : @"19957253180",
                                        @"bFatherPhone" : @"2222222",
                                        @"husbFamHistory" : @"3333333",
                                        @"bFatherPcId" : @"228384848484944",
                                        @"isHusbFamHistory" : @"1",
                                        @"bFatherJob" : @"01",
                                        @"bFatherHealth" : @"1",
                                        @"bFatherName" : @"sxk"
                                        },
                                
                                @"pregnancyHistorys" : @{
                                        @"pregnancyHistory" : @[
                                                @{
                                                    @"zycqk" : @"01",
                                                    @"historyId" : @"210585",
                                                    @"birthTime" : @"2019-04",
                                                    @"bearType" : @"01",
                                                    @"chhfqk" : @"01",
                                                    @"babyInfos" : @{
                                                            @"babyInfo" : @[
                                                                    @{
                                                                        @"babysex" : @"01",
                                                                        @"babyWeight" : @"3000",
                                                                        @"babyStatus" : @"01",
                                                                        @"ycrdn" : @"210585"
                                                                        },
                                                                    @{
                                                                        @"babysex" : @"01",
                                                                        @"babyWeight" : @"3000",
                                                                        @"babyStatus" : @"01",
                                                                        @"ycrdn" : @"210585"
                                                                        }
                                                                    ]
                                                            }
                                                    },
                                                @{
                                                    @"zycqk" : @"01",
                                                    @"historyId" : @"210586",
                                                    @"birthTime" : @"2019-04",
                                                    @"bearType" : @"01",
                                                    @"chhfqk" : @"01",
                                                    @"babyInfos" : @{
                                                            @"babyInfo" : @[
                                                                    @ {
                                                                        @"babysex" : @"01",
                                                                        @"babyWeight" : @"3000",
                                                                        @"babyStatus" : @"01",
                                                                        @"ycrdn" : @"210586"
                                                                    }
                                                                    ]
                                                            }
                                                    },
                                                ]
                                        },
                                };
複製代碼

此數據包括四大模塊:數組

1.基本信息-字符串bash

2.我的信息-personalMessage(字典)app

3.小孩父親資料-babyFatherInfo(字典)ui

4.歷史懷孕詳情-pregnancyHistorys(字典包含一個大的數組pregnancyHistory,數組包含n個字典,每一個字典包含基本信息以及n個胎數)atom

接下來咱們構建model類:spa

#import <Foundation/Foundation.h>
@class YYModel_PersonalModel,PersonalMessageModel,BabyFatherInfoModel,PregnancyHistorys,BabyInfos,BabyInfoModel;
NS_ASSUME_NONNULL_BEGIN

@interface YYModel_PersonalModel : NSObject

@property(nonatomic,copy) NSString *birthPlace;
@property(nonatomic,copy) NSString *education;
@property(nonatomic,copy) NSString *personId;
@property(nonatomic,strong) PersonalMessageModel *personalMessage;
@property(nonatomic,strong) BabyFatherInfoModel *babyFatherInfo;


@end

@interface PersonalMessageModel : NSObject

@property(nonatomic,copy) NSString *marryAge;
@property(nonatomic,copy) NSString *marryStatus;
@property(nonatomic,copy) NSString *paddress;
@property(nonatomic,copy) NSString *phone;
@property(nonatomic,copy) NSString *pname;

@end

@interface BabyFatherInfoModel : NSObject

@property(nonatomic,copy) NSString *bFatherAge;
@property(nonatomic,copy) NSString *bFatherUnit;
@property(nonatomic,copy) NSString *bFatherPhone;
@property(nonatomic,copy) NSString *bFatherJob;
@property(nonatomic,copy) NSString *bFatherName;

@end

@interface PregnancyHistorys : NSObject

@property(nonatomic,copy) NSString *zycqk;
@property(nonatomic,copy) NSString *birthTime;
@property(nonatomic,copy) NSString *historyId;
@property(nonatomic,copy) NSString *bearType;
@property(nonatomic,strong) BabyInfos *babyInfos;

@end

@interface BabyInfos : NSObject

@property(nonatomic,strong)NSArray *babyInfo;


@end

@interface BabyInfoModel : NSObject

@property(nonatomic,copy) NSString *babyWeight;
@property(nonatomic,copy) NSString *babyStatus;
@property(nonatomic,copy) NSString *babysex;
@property(nonatomic,copy) NSString *ycrdn;


@end
NS_ASSUME_NONNULL_END
複製代碼

使用YYModel解析以下code

首先咱們解析除pregnancyHistorys意外全部數據字符串

在model .m文件使用以下方法string

// 若是實現了該方法,則處理過程當中會忽略該列表內的全部屬性-黑名單
+ (NSArray *)modelPropertyBlacklist {
    return @[@"pregnancyHistorys"];
}
複製代碼

因爲基本信息出現了id關鍵字、model .m文件中特做以下處理:

// 當後臺返回系統關鍵字時、屬性數據映射,用來定義多樣化數據時轉換聲明
+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
             @"personId":@"id",
             };
}
複製代碼

執行了此方法,就能夠將id轉變爲personID。

解析過程以下:(註明了YYModel經常使用的方法、這裏不一一解釋)

NSDictionary *dictionary = [[YYModelJsonData shareInstance]dataDictionary];
//字典轉模型 + (nullable instancetype)yy_modelWithDictionary:(NSDictionary *)dictionary
YYModel_PersonalModel *model = [YYModel_PersonalModel yy_modelWithDictionary:dictionary];
NSLog(@"出生地:%@、教育水平:%@、personID:%@、我的信息-結婚年齡:%@、小孩父親:%@",model.birthPlace,model.education,model.personId,model.personalMessage.marryAge,model.babyFatherInfo.bFatherName);
    
//模型轉json字符串 yy_modelToJSONString
NSString *json = [model yy_modelToJSONString];
NSLog(@"json:%@",json);
    
// 模型轉NSObject yy_modelToJSONObject
NSDictionary *returnDic = [model yy_modelToJSONObject];
NSLog(@"returnValue:%@",returnDic);
    
// 模型轉NSData yy_modelToJSONData
NSData *dataJson = [model yy_modelToJSONData];
NSLog(@"dataJson:%@",dataJson);
複製代碼

打印結果以下(只打印個別字段):

2019-04-24 10:53:40.561403+0800 RepeatUnit_iOS[2353:78795] 出生地:寧夏固原市彭陽縣草廟鄉、教育水平:博士、personID:11五、我的信息-結婚年齡:30、小孩父親:sxk
2019-04-24 10:53:40.562234+0800 RepeatUnit_iOS[2353:78795] json:{"id":"115","personalMessage":{"phone":"158*****289","marryStatus":"1","marryAge":"30","paddress":"寧夏固原市","pname":"sxk"},"babyFatherInfo":{"bFatherJob":"01","bFatherAge":"38","bFatherPhone":"2222222","bFatherUnit":"19957253180","bFatherName":"sxk"},"birthPlace":"寧夏固原市彭陽縣草廟鄉","education":"博士"}
2019-04-24 10:53:40.562575+0800 RepeatUnit_iOS[2353:78795] returnValue:{
    babyFatherInfo =     {
        bFatherAge = 38;
        bFatherJob = 01;
        bFatherName = sxk;
        bFatherPhone = 2222222;
        bFatherUnit = 19957253180;
    };
    birthPlace = "\U5b81\U590f\U56fa\U539f\U5e02\U5f6d\U9633\U53bf\U8349\U5e99\U4e61";
    education = "\U535a\U58eb";
    id = 115;
    personalMessage =     {
        marryAge = 30;
        marryStatus = 1;
        paddress = "\U5b81\U590f\U56fa\U539f\U5e02";
        phone = "158*****289";
        pname = sxk;
    };
}
2019-04-24 10:53:40.562824+0800 RepeatUnit_iOS[2353:78795] dataJson:<7b226964 223a2231 3135222c 22706572 736f6e61 6c4d6573 73616765 223a7b22 70686f6e 65223a22 3135382a 2a2a2a2a 32383922 2c226d61 72727953 74617475 73223a22 31222c22 6d617272 79416765 223a2233 30222c22 70616464 72657373 223a22e5 ae81e5a4 8fe59bba e58e9fe5 b882222c 22706e61 6d65223a 2273786b 227d2c22 62616279 46617468 6572496e 666f223a 7b226246 61746865 724a6f62 223a2230 31222c22 62466174 68657241 6765223a 22333822 2c226246 61746865 7250686f 6e65223a 22323232 32323232 222c2262 46617468 6572556e 6974223a 22313939 35373235 33313830 222c2262 46617468 65724e61 6d65223a 2273786b 227d2c22 62697274 68506c61 6365223a 22e5ae81 e5a48fe5 9bbae58e 9fe5b882 e5bdade9 98b3e58e bfe88d89 e5ba99e4 b9a1222c 22656475 63617469 6f6e223a 22e58d9a e5a3ab22 7d>
複製代碼

如今咱們繼續解析歷史孕次(pregnancyHistorys),跟上面解析過程一模一樣:

NSArray *history = [[dictionary objectForKey:@"pregnancyHistorys"]objectForKey:@"pregnancyHistory"];
//model數組集合 傳入model類以及json數據
NSArray *historyModelArray = [NSArray yy_modelArrayWithClass:[PregnancyHistorys class] json:history];
NSLog(@"-%@",historyModelArray);
    
//第一個數據爲例、實際應用中會根據indexPath等參數對應
PregnancyHistorys *historyModel = historyModelArray[0];
NSLog(@"小孩編號:%@---小孩infos:%@",historyModel.zycqk,historyModel.babyInfos.babyInfo);
   
//第一個數據爲例、實際應用中會根據indexPath等參數對應
BabyInfoModel *babyInfoModel = [BabyInfoModel yy_modelWithDictionary:historyModel.babyInfos.babyInfo[0]];
NSLog(@"小孩體重:%@",babyInfoModel.babyWeight);
複製代碼

打印結果以下(只打印個別字段):

2019-04-24 10:53:40.573012+0800 RepeatUnit_iOS[2353:78795] -(
    "<PregnancyHistorys: 0x600001209d40>",
    "<PregnancyHistorys: 0x600001209ec0>"
)
2019-04-24 10:53:40.573215+0800 RepeatUnit_iOS[2353:78795] 小孩編號:01---小孩infos:(
        {
        babyStatus = 01;
        babyWeight = 3000;
        babysex = 01;
        ycrdn = 210585;
    },
        {
        babyStatus = 01;
        babyWeight = 3000;
        babysex = 01;
        ycrdn = 210585;
    }
)
2019-04-24 10:53:40.573562+0800 RepeatUnit_iOS[2353:78795] 小孩體重:3000
複製代碼

另外,YYModel還提供了白名單:

若是實現了該方法,則處理過程當中不會處理該列表外的屬性-白名單
假如只須要解析我的信息時,返回personalMessage便可
+ (NSArray *)modelPropertyWhitelist {
    return @[@"personalMessage"];
}
複製代碼

經過以上解析過程、咱們基本掌握了YYModel常見的方法,解析之後,也能拿到本身想要的數據。具體處理過程還須要結合UI需求。

相關文章
相關標籤/搜索