YYModel 사용

14776 단어
iOS 개발 에 있어 서 각종 JSON 과 모델 이 서로 전환 하 는 수 요 를 사용 할 수 있 습 니 다. 이전 프로젝트 에서 계속 사용 MJExtension 했 지만 최근 에 경량급 라 이브 러 리 YYModel 를 발 견 했 습 니 다. 사용 이 간단 하고 성능 도 좋 습 니 다. 다음은 YYModel 의 간단 한 용법 과 주의사항 을 말씀 드 리 겠 습 니 다.
  • JSON 모델 로 전환 하거나 모델 이 JSON 로 전환 하 는 예
  • //JSON
    {
        accountId = "";
        avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
        unionId = o2t87wKAR1pNRZR0cnReBT608mok;
        userid = 11703346;
        wxNickName = "\U90a3\U5496";
    }
    //Model
    @interface MyFansModel : NSObject
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @end
    //JSON     
    MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
    MyFansModel *fansModel = [MyFansModel yy_modelWithDictionary:dic];
    
    //     JSON
    NSDictionary *json = [fansModel yy_modelToJSONObject];
    
  • 수 동 으로 만 든 모델 과 서버 에서 되 돌아 오 는 모델 속성 이름 이 일치 하지 않 을 때의 예
  • //JSON
    { "merchantId" = "";
      "avatar" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
      "unionID" = "o2t87wKAR1pNRZR0cnReBT608mok";
      "userID" = "11703346";
      "userName" = "   ";
    }
    //Model
    //.h 
    #import 
    @interface MyFansModel : NSObject 
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @end
    //.m 
    + (nullable NSDictionary *)modelCustomPropertyMapper
    {
        return @{@"avatarUrl" : @"avatar",
                 @"wxNickName" : @"userName",
                 @"accountId" : @"merchantId",
                 @"unionId" : @"unionID",
                 @"userid":@"userID",
                 };
    }
    MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
    MyFansModel *fansModel = [MyFansModel yy_modelWithDictionary:dic];
    
  • 모델 세트 모델 의 예
  • //JSON
    {
        citys =   (
                    {
                area = "";
                areaId = "";
                areas =             (
                                    {
                        area = "\U4e1c\U57ce\U533a";
                        cityid = 110100;
                        id = 110101;
                        status = 1;
                    },
                                    {
                        area = "\U897f\U57ce\U533a";
                        cityid = 110100;
                        id = 110102;
                        status = 1;
                    },
                                    {
                        area = "\U5d07\U6587\U533a";
                        cityid = 110100;
                        id = 110103;
                        status = 1;
                    },
                                    {
                        area = "\U5ba3\U6b66\U533a";
                        cityid = 110100;
                        id = 110104;
                        status = 1;
                    },
                                    {
                        area = "\U671d\U9633\U533a";
                        cityid = 110100;
                        id = 110105;
                        status = 1;
                    },
                                    {
                        area = "\U4e30\U53f0\U533a";
                        cityid = 110100;
                        id = 110106;
                        status = 1;
                    },
                                    {
                        area = "\U77f3\U666f\U5c71\U533a";
                        cityid = 110100;
                        id = 110107;
                        status = 1;
                    },
                                    {
                        area = "\U6d77\U6dc0\U533a";
                        cityid = 110100;
                        id = 110108;
                        status = 1;
                    },
                                    {
                        area = "\U95e8\U5934\U6c9f\U533a";
                        cityid = 110100;
                        id = 110109;
                        status = 1;
                    },
                                    {
                        area = "\U623f\U5c71\U533a";
                        cityid = 110100;
                        id = 110111;
                        status = 1;
                    },
                                    {
                        area = "\U901a\U5dde\U533a";
                        cityid = 110100;
                        id = 110112;
                        status = 1;
                    },
                                    {
                        area = "\U987a\U4e49\U533a";
                        cityid = 110100;
                        id = 110113;
                        status = 1;
                    },
                                    {
                        area = "\U660c\U5e73\U533a";
                        cityid = 110100;
                        id = 110114;
                        status = 1;
                    },
                                    {
                        area = "\U5927\U5174\U533a";
                        cityid = 110100;
                        id = 110115;
                        status = 1;
                    },
                                    {
                        area = "\U6000\U67d4\U533a";
                        cityid = 110100;
                        id = 110116;
                        status = 1;
                    },
                                    {
                        area = "\U5e73\U8c37\U533a";
                        cityid = 110100;
                        id = 110117;
                        status = 1;
                    }
                );
                city = "\U5e02\U8f96\U533a";
                cityId = "";
                id = 110100;
                province = "";
                provinceid = 110000;
                status = 1;
            },
                    {
                area = "";
                areaId = "";
                areas =             (
                                    {
                        area = "\U5bc6\U4e91\U53bf";
                        cityid = 110200;
                        id = 110228;
                        status = 1;
                    },
                                    {
                        area = "\U5ef6\U5e86\U53bf";
                        cityid = 110200;
                        id = 110229;
                        status = 1;
                    }
                );
                city = "\U53bf";
                cityId = "";
                id = 110200;
                province = "";
                provinceid = 110000;
                status = 1;
            }
        );
        id = 110000;
        province = "\U5317\U4eac\U5e02";
        status = 1;
    }
    //Model
    //.h
    //BaseModel      id   _id 
    #import 
    NS_ASSUME_NONNULL_BEGIN
    
    @interface BaseModel : NSObject 
    
    /// id     _id
    @property (nonatomic, copy) NSString *_id;
    
    @end
    //.m
    +(NSDictionary *)modelCustomPropertyMapper
    {
        return @{@"_id": @"id"};
    }
    //        .h  
    @interface AreaModel : BaseModel
    @property (nonatomic, copy) NSString *area;
    @property (nonatomic, copy) NSString *cityid;
    @property (nonatomic, copy) NSString *status;
    @end
    
    @interface CityModel : BaseModel
    @property (nonatomic, copy) NSString *city;
    @property (nonatomic, copy) NSString *provinceid;
    @property (nonatomic, copy) NSString *status;
    @property (nonatomic, strong) NSArray *areas;
    @end
    
    @interface ProvinceModel : BaseModel
    
    @property (nonatomic, copy) NSString *province;
    @property (nonatomic, copy) NSString *status;
    @property (nonatomic, strong) NSArray *citys;
    
    @end
    //.m   
    @implementation AreaModel
    
    @end
    
    @implementation CityModel
    + (NSDictionary *)modelContainerPropertyGenericClass {
        return @{@"areas" : [AreaModel class]};
    }
    @end
    
    @implementation ProvinceModel
    
    + (NSDictionary *)modelContainerPropertyGenericClass {
        return @{@"citys" : [CityModel class]};
    }
    @end
    
    // JSON     
    ProvinceModel *provinceModel = [ProvinceModel yy_modelWithJSON:dic];
    CityModel *cityModel = provinceModel.citys[0];
    AreaModel *areaModel = cityModel.areas[0];
    
  • 배열 에 사전 형식 을 모델 로 바 꾸 는 예
  • //JSON
    (
    {
    "accountId" = "";
    "avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    "unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
    "userid" = "11703346",
    "wxNickName" = "   "
    },
    {
    "accountId" = "";
    "avatarUrl" = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
    "unionId" = "o2t87wKAR1pNRZR0cnReBT608mok";
    "userid" = "11293020";
    "wxNickName" = "  ";
    }
    );
    //Model
    @interface MyFansModel : NSObject
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @end
    //     
    NSArray *myFansModelArray = [NSArray yy_modelArrayWithClass:[MyFansModel class] json:array];
    
  • 용기 류 속성
  • //JSON
    {
    "schoolName" = "  ";
    "teachers" =  (
          {"teacherName"= "Sndday","teacherAge" = 50};
          {"teacherName" = "Ssasas","teacherAge" = 70};
          {"teacherName" = "Snddasa","teacherAge"= 30)}
          );
    };
    //Model
    //.h
    #import 
    
    
    @interface SchoolModel : NSObject 
    
    @property (nonatomic, copy) NSString *schoolName;
    @property (nonatomic, copy) NSArray *teachers;
    
    @end
    //.m
    #import "TeacherModel.h"
    
    @implementation SchoolModel
    
    + (nullable NSDictionary *)modelContainerPropertyGenericClass
    {
        return @{@"teachers":[TeacherModel class]};
    }
    
    @end
    //TeacherModel
    
    @interface TeacherModel : NSObject
    
    @property (nonatomic, copy) NSString *teacherName;
    @property (nonatomic, assign) NSInteger teacherAge;
    
    @end
    
    //     
    NSDictionary *dic = @{
      @"schoolName":@"  ",
      @"teachers":@[
              @{@"teacherName":@"Sndday",@"teacherAge":@(50)},
              @{@"teacherName":@"Ssasas",@"teacherAge":@(70)},
              @{@"teacherName":@"Snddasa",@"teacherAge":@(30)}
              ],
      };
    
    SchoolModel *model = [SchoolModel yy_modelWithJSON:dic];
    
    TeacherModel *teacherModel = model.teachers[0];
    
    
  • 블랙리스트, 즉 블랙리스트 에 가입 한 필드 는 무시 되 고 전환 되 지 않 습 니 다
  • //JSON
    {
        accountId = "";
        avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
        unionId = o2t87wKAR1pNRZR0cnReBT608mok;
        userid = 11703346;
        wxNickName = "\U90a3\U5496";
    }
    //Model
    //.h
    #import 
    @interface MyFansModel : NSObject 
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @end
    //.m
    + (NSArray *)modelPropertyBlacklist 
    {
        return @[@"avatarUrl", @"accountId"];
    }
    // JSON     ,         ```avatarUrl``` ```accountId```   ```nil```
    MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
    
  • 화이트 리스트 는 화이트 리스트 에 가입 한 필드 만 처리 하고 나머지 필드 는 무시 합 니 다
  • //JSON
    {
        accountId = "";
        avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
        unionId = o2t87wKAR1pNRZR0cnReBT608mok;
        userid = 11703346;
        wxNickName = "\U90a3\U5496";
    }
    //Model
    //.h
    #import 
    @interface MyFansModel : NSObject 
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @end
    //.m
    + (NSArray *)modelPropertyWhitelist 
    {
        return @[@"avatarUrl", @"accountId"];
    }
    // JSON     ,         ```avatarUrl``` ```accountId```   ```nil```
    MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
    
  • 데이터 검증 과 사용자 정의 전환
  • {
        accountId = "";
        avatarUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/sqicFCgiaheqCFbjrYgTmwXUQOB5l5Iyo47cVVp2cLlHARck6XgXscqicJ2ZVibicCbGH4iaVcQz8ptXqbV6n9ic5iaL9g/132";
        unionId = o2t87wKAR1pNRZR0cnReBT608mok;
        userid = 11703346;
        wxNickName = "\U90a3\U5496";
    }
    #import 
    @interface MyFansModel : NSObject 
    @property (nonatomic, strong) NSString *avatarUrl;
    @property (nonatomic, strong) NSString *wxNickName;
    @property (nonatomic, strong) NSString *accountId;
    @property (nonatomic, strong) NSString *unionId;
    @property (nonatomic, strong) NSString *userid;
    @property (nonatomic, strong) NSString *resultStr;
    @end
    //.m
    - (BOOL)modelCustomTransformFromDictionary:(NSDictionary *)dic {
        NSString *timestamp = dic[@"accountId"];
        if ([timestamp isEqualToString:@""]) return NO;
        _resultStr = [NSString stringWithFormat:@"   %@",timestamp];
        return YES;
    }
    // JSON     
        MyFansModel *fansModel = [MyFansModel yy_modelWithJSON:dic];
    

    상기 예 는 accountId 에 대한 검 사 를 했 기 때문에 accountId 요구 에 부합 되 지 않 으 면 이 JSON 는 해석 되 지 않 을 것 이다. 물론 백 엔 드 에서 되 돌아 오 는 시간 스탬프 를 시간 문자열 로 바 꾸 는 등 사용자 정의 작업 도 할 수 있다.
    이로써 YYModel 에 관 한 용법 은 모두 말 했 으 며, 어떤 말 이 잘못 되 었 다 면 지적 을 환영 합 니 다.마음 에 드 시 면 좋아요 눌 러 주세요!!

    좋은 웹페이지 즐겨찾기