TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jul 2017 |
Maintained by CSModel.
CSModel is a concise and efficient model framework for iOS/OSX, and provides nested Model to compare values and copy values.
The time cost of disposing 10000 times GithubUser objects (iPhone 6s).
// 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];
}
CSModel is released under the MIT license. See LICENSE for details.