CSModel 0.1.0

CSModel 0.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年7月

CSModel 维护。



CSModel 0.1.0

  • Chasel-Shao

   

📖英文文档 | 📖 中文文档

介绍

CSModel 是一个简洁高效的 iOS/OSX 模型框架,并提供嵌套模型来进行值比较和值复制。

特性

  • 轻量级:易于使用,源文件少
  • 侵入性低:不需要继承其他类
  • 类型安全:检查对象的每个类型,并处理 JSON 中的空值
  • 高性能:解析 JSON 非常快,并支持嵌套模型
  • 比较:支持值比较,可以处理多层嵌套模型
  • 复制:提供从另一个模型到嵌套模型的复制功能

性能

dispose 10,000 次 GithubUser 对象的时间消耗(iPhone 6s)。

Benchmark result

入门

JSON、模型和字符串之间的转换

// JSON:
{
    "uid":8082,
    "name":"Amy",
    "age":23
}

// Model:
@interface Person : NSObject
@property (nonatomic,assign) UInt64 uid;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) NSInteger age;
@end
@implementation Person
@end
	
// 1. Converting the JSON to an Model:
Person *p = [Person cs_modelWithJSONObject:json];

// 2. Converting the String to an Model:
 Person *np = [Person cs_modelWithJSONString:jsonStr];
	
// 3. Converting the Model to an JSON:
id json = [p cs_JSONObject];

// 4. Converting the Model to an NSString:
NSString *jsonStr =  [p cs_JSONString];

// 5. Converting the JSON Array to an Model Array:
NSArray *array = [Person cs_modelArrayWithJSONObject:jsonArray];

如何使用协议

// 1. If a value of key in the json is an array,The json array will be
// conveted to model array by implementing this method.
+ (NSDictionary<NSString *,NSString *> *)CSModelKeyWithPropertyMapping
    return @{@"user_id":@"userId"};
}

// 2. If a value of key is a JSON object , implement this method to convert
// the JSON object to a model's properites.
+ (NSDictionary<NSString *,Class> *)CSModelArrayWithModelMapping{
    return @{@"child":[Person class]};
}

// 3. The mapping of model property and json key
+ (NSDictionary<NSString *,Class> *)CSModelDictionaryKeyWithModelMapping{
    return @{@"child":[Person class]};
}

比较和复制方法

// Model 
@interface Teacher : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,copy)NSString *books;
@end
@implementation Teacher
@end

// 1. Comparing the value of two models, supported the nested Model:
BOOL isSame = [p cs_isEqualToValue:p2];

// 2. Copying the value of an model, supported the nested Model:
Person *p2 = [p cs_modelCopy];

// 3. Copying from the different model:
Teacher *teacher1 = [Teacher cs_modelCopyFromModel:p];

描述方法

// Implementing the method in the `.m file` of the Model
-(NSString *)description{
    return [self cs_description];
}

编码方式

// Implementing the following method in the `.m file` of the Model
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [self cs_encode:aCoder];
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    return [self cs_decoder:aDecoder];
}

安装

手动安装

  1. 下载 CSModel 源代码文件
  2. 导入 CSModel.h 和相关源文件

作者

许可证

CSModel采用MIT许可发布。详情请参阅LICENSE文件。