Git Product home page Git Product logo

genericmodel's Introduction

GenericModel

Objective-C Model、JSON、NSDictionary互相转换简单高效的轻量级框架,支持model嵌套model。

Features

  • GenericModel 支持Objective-C ModelNSDictionaryJSON之间互相转换,框架非常简单高效,内部字段反射设置有缓存,用Objective-C中的Protocol限定NSArrayNSDictionary容器类的类型,防止容器类型变量类型使用错误,类似Java中容器类型的泛型
  • 支持的类型转换
    • NSDictionary <--> Model
    • JSON <--> Model

Example

NSDictionary -> Model

字典类型转换简单Model

//StudentModel.h
@protocol StudentModel @end
@interface StudentModel : GenericModel

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *hobby;
@property (nonatomic, assign) NSInteger age;

@end
//Example Code 
NSDictionary *studentDic = @{
                             @"name" : @"Name1",
                             @"hobby": @"Basketball",
                             @"age"  : @(25)};
 
StudentModel *studentModel = [GenericModel
                              getObjectByDictionary:studentDic
                              clazz:[StudentModel class]];
                              
NSLog(@"studentModel:name:%@, hobby:%@, age:%ld",studentModel.name, studentModel.hobby, (long)studentModel.age);
//Output
studentDic:{
    age = 13;
    hobby = Football;
    name = Name2;
}

Model -> NSDictionary

简单Model转换成字典

//Example Code 
StudentModel *student = [[StudentModel alloc] init];
student.name = @"Name2";
student.hobby = @"Football";
student.age = 13;
NSDictionary *studentDic = [GenericModel getDictionaryByObject:student];
 
NSLog(@"studentDic:%@",studentDic.description);
//Output
gradeDic:{
    students =     (
                {
            age = 15;
            hobby = BasketBall;
            name = "student_1";
        },
                {
            age = 14;
            hobby = Football;
            name = "student_2";
        }
    );
}

(Model Include NSSArray) -> NSDictionary

带有数组的Model转换成字典

//Example Code 
StudentModel *student_1 = [[StudentModel alloc] init];
student_1.name = @"student_1";
student_1.hobby = @"BasketBall";
student_1.age = 15;
    
StudentModel *student_2 = [[StudentModel alloc] init];
student_2.name = @"student_2";
student_2.hobby = @"Football";
student_2.age = 14;
    
GradeModel *gradeMode = [[GradeModel alloc] init];
[gradeMode.students addObject:student_1];
[gradeMode.students addObject:student_2];
    
NSDictionary *gradeDic = [GenericModel getDictionaryByObject:gradeMode];
NSLog(@"gradeDic:%@",gradeDic.description);
//Output
gradeDic:{
    students =     (
                {
            age = 15;
            hobby = BasketBall;
            name = "student_1";
        },
                {
            age = 14;
            hobby = Football;
            name = "student_2";
        }
    );
}

NSDictionary --> (Model Include NSSArray)

字典转换成带有数组的Model

//Example Code 
NSDictionary *gradeDic2 = @{
                            @"students" : @[
                                    @{
                                        @"name" : @"Name1",
                                        @"hobby": @"Football",
                                        @"age"  : @(13)},
                                    @{
                                        @"name" : @"Name2",
                                        @"hobby": @"Basketball",
                                        @"age"  : @(14)},
                                    @{
                                        @"name" : @"Name3",
                                        @"hobby": @"Basketball",
                                        @"age"  : @(15)}]
                            };
GradeModel *gradeMode2 = [GenericModel getObjectByDictionary:gradeDic2 clazz:[GradeModel class]];
for (StudentModel *mode in gradeMode2.students) {
    NSLog(@"studentModel:name:%@, hobby:%@, age:%ld",mode.name, mode.hobby, (long)mode.age);
}
//Output
2015-05-01 17:29:37.673 GenericModel[14890:282213] studentModel:name:Name1, hobby:Football, age:13
2015-05-01 17:29:37.673 GenericModel[14890:282213] studentModel:name:Name2, hobby:Basketball, age:14
2015-05-01 17:29:37.673 GenericModel[14890:282213] studentModel:name:Name3, hobby:Basketball, age:15

(Model Include NSDictionary) --> NSDictionary

带有字典的复杂model转化成字典

//Example Code 
FriendsModel *friends = [[FriendsModel alloc] init];
NSDictionary *tempFriendsDic = @{
                                 @"friend1" : @{
                                         @"name" : @"Name1",
                                         @"hobby": @"Football",
                                         @"age"  : @(13)},
                                 @"friend2" : @{
                                         @"name" : @"Name2",
                                         @"hobby": @"Basketball",
                                         @"age"  : @(14)},
                                 @"friend3" : @{
                                         @"name" : @"Name3",
                                         @"hobby": @"Basketball",
                                         @"age"  : @(15)}
                                 };
friends.friendDic = (NSMutableDictionary<StudentModel> *)tempFriendsDic;
NSDictionary *friendsDic = [GenericModel getDictionaryByObject:friends];
NSLog(@"friendsDic:%@",friendsDic);
//Output
friendsDic:{
    friendDic =     {
        friend1 =         {
            age = 13;
            hobby = Football;
            name = Name1;
        };
        friend2 =         {
            age = 14;
            hobby = Basketball;
            name = Name2;
        };
        friend3 =         {
            age = 15;
            hobby = Basketball;
            name = Name3;
        };
    };
}

((model extended by other model) -> NSDictionary

继承与其他model的model转化成字典

#import "StudentModel.h"

@interface SubStudentModel : StudentModel

@property (nonatomic, copy) NSString *birthName;

@end
//Example Code 
SubStudentModel *subStudentModel = [[SubStudentModel alloc] init];
subStudentModel.name = @"student_1";
subStudentModel.hobby = @"BasketBall";
subStudentModel.age = 15;
subStudentModel.birthName = @"birthName1";
NSDictionary *subStudentDic = [GenericModel getDictionaryByObject:subStudentModel];
NSLog(@"subStudentDic:%@",subStudentDic);
//Output
subStudentDic:{
    age = 15;
    birthName = birthName1;
    hobby = BasketBall;
    name = "student_1";
}

NSDictionary --> (model extended by other model)

字典转化成继承与其他model的model

//Example Code 
NSDictionary *subStudentDic = @{
                             @"name" : @"Name1",
                             @"hobby": @"Basketball",
                             @"age"  : @(25),
                             @"birthName" : @"birthName1"};
    
SubStudentModel *subStudentModel = [GenericModel getObjectByDictionary:subStudentDic
                                                                 clazz:[SubStudentModel class]];
NSLog(@"studentModel:name:%@, hobby:%@, age:%ld , birthName:%@",
      subStudentModel.name,
      subStudentModel.hobby,
      (long)subStudentModel.age,
      subStudentModel.birthName);
//Output
2015-05-01 17:29:37.672 GenericModel[14890:282213] studentModel:name:Name1, hobby:Basketball, age:25

genericmodel's People

Contributors

zyma678 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.