Latest 1.1.2
Homepage https://github.com/csdq/CSDataModel
License GPL-3.0
Platforms ios 8.0
Authors

一个简单的数据模型基类,方便将NSDictionary、NSArray等类型数据转为模型对象

基本使用

例如一个JSON对象Department

{
"name": "DepartmentA",
"tel": "88888888",
"num": 1
}

该JSON String转化成NSDictionary: dict1

模型:

@interface Department : CSBaseModel
@property (nonatomic , strong) NSString *name;
@property (nonatomic , strong) NSString *tel;
@property (nonatomic , strong) NSNumber *num;
@end

转化时:

Department *deprtmt = [Department modelFromDict:dict1];

嵌套使用

模型

对于常见的情况:

{
"name": "departmentA",
"number": 3,
"members": [{
"name": "王五",
"jobNum": "0023"
},
{
"name": "陈六",
"jobNum": "0028"
}]
}

该JSON String转化成NSDictionary: dict2

Department中含有成员Member

@interface Department : CSBaseModel
@property (nonatomic , strong) NSString *name;
@property (nonatomic , strong) NSArray<Member*> *members;
@end

@interface Member : CSBaseModel
@property (nonatomic , strong) NSString *name;
@property (nonatomic , strong) NSNumber *jobNum;
@end

Deparmtent实现文件中需要注册子模型的类

@implementation Department
-(instancetype)init{
self = [super init];
if (self) {
[self registerClass:[Member class]   forProperty:@"members"];
}
return self;
}
@end

转化时:

Department *deprtmt = [Department modelFromDict:dict2];

转化成功后,可通过deprtmt.members访问member数组

Latest podspec

{
    "name": "CSDataModel",
    "version": "1.1.2",
    "summary": "u4e00u4e2au7b80u5355u7684u6570u636eu6a21u578bu57fau7c7buff0cu65b9u4fbfu5c06NSDictionaryu3001NSArrayu7b49u7c7bu578bu6570u636eu8f6cu4e3au6a21u578bu5bf9u8c61",
    "description": "# CSDataModelnnu4e00u4e2au7b80u5355u7684u6570u636eu6a21u578bu57fau7c7buff0cu65b9u4fbfu5c06NSDictionaryu3001NSArrayu7b49u7c7bu578bu6570u636eu8f6cu4e3au6a21u578bu5bf9u8c61nn## u57fau672cu4f7fu7528nnu4f8bu5982u4e00u4e2aJSONu5bf9u8c61Departmentnn{n"name": "DepartmentA",n"tel": "88888888",n"num": 1n}nnu8be5JSON Stringu8f6cu5316u6210NSDictionaryuff1a dict1nn### [email protected] Department : [email protected] (nonatomic , strong) NSString *name;[email protected] (nonatomic , strong) NSString *tel;[email protected] (nonatomic , strong) NSNumber *num;[email protected]### u8f6cu5316u65f6uff1annDepartment *deprtmt = [Department modelFromDict:dict1];nn## u5d4cu5957u4f7fu7528nn### u6a21u578bnu5bf9u4e8eu5e38u89c1u7684u60c5u51b5uff1ann{n"name": "departmentA",n"number": 3,n"members": [{n"name": "u738bu4e94",n"jobNum": "0023"n},n{n"name": "u9648u516d",n"jobNum": "0028"n}]n}nnu8be5JSON Stringu8f6cu5316u6210NSDictionaryuff1a [email protected] Department : [email protected] (nonatomic , strong) NSString *name;[email protected] (nonatomic , strong) NSArray *members;[email protected]@interface Member : [email protected] (nonatomic , strong) NSString *name;[email protected] (nonatomic , strong) NSNumber *jobNum;[email protected][email protected] Departmentn-(instancetype)init{nself = [super init];nif (self) {n[self registerClass:[Member class]   forProperty:@"members"];n}nreturn self;n}[email protected]### u8f6cu5316u65f6uff1annDepartment *deprtmt = [Department modelFromDict:dict2];nnu8f6cu5316u6210u529fu540euff0cu53efu901au8fc7deprtmt.membersu8bbfu95eememberu6570u7ec4",
    "homepage": "https://github.com/csdq/CSDataModel",
    "license": {
        "type": "GPL-3.0",
        "file": "LICENSE"
    },
    "authors": {
        "Mr.s": "[email protected]"
    },
    "source": {
        "git": "https://github.com/csdq/CSDataModel.git",
        "tag": "1.1.2"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "CSDataModel/Classes/**/*"
}

Pin It on Pinterest

Share This