测试已测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最新发布 | 2016年10月 |
SPM支持 SPM | ✓ |
由 Florion Coiffe 维护。
将 Core Data 对象映射到由您提供的后端提供的 JSON,从未如此简单。
{
"firstName": "John",
"lastName": "Hyperseed"
}
NSDictionary *values = [JSON valueForKey:@"user"];
[user hyp_fillWithDictionary:values];
您的 Core Data 实体应与后端模型相匹配。您的属性应与其 JSON 对应属性相匹配。例如 firstName
映射到 firstName
,address
映射到 address
。
此规则有两个例外
id
应该与 remoteID
匹配entityName
为前缀(type
变为 userType
,description
变为 userDescription
等)。在 JSON 中它们无需更改,例如您可以保留 type
和 description
。有关完整保留属性列表,请参阅此处{
"first_name": "John",
"last_name": "Hyperseed"
}
NSDictionary *values = [JSON valueForKey:@"user"];
[user hyp_fillWithDictionary:values];
您的 Core Data 实体应与后端模型相匹配,但应为 camelCase
。您的属性应与其 JSON 对应属性相匹配。例如 first_name
映射到 firstName
,address
映射到 address
。
此规则有两个例外
id
应该与 remoteID
匹配entityName
为前缀(type
变为 userType
,description
变为 userDescription
等)。在 JSON 中它们无需更改,例如您可以保留 type
和 description
。有关完整保留属性列表,请参阅此处hyper.remoteKey
并输入要映射的值来实现。对于映射数组和字典,只需在 Core Data 模型器中将属性设置为 二进制数据
我们直接支持 ISO 8601 和 unix 时间戳,因为这两种格式是解析日期时最常用的格式,同时我们有一个非常高效的解析字符串的方式,这可以克服使用 NSDateFormatter
的 性能问题。
NSDictionary *values = @{@"created_at" : @"2014-01-01T00:00:00+00:00",
@"updated_at" : @"2014-01-02",
@"published_at": @"1441843200"
@"number_of_attendes": @20};
[managedObject hyp_fillWithDictionary:values];
NSDate *createdAt = [managedObject valueForKey:@"createdAt"];
// ==> "2014-01-01 00:00:00 +00:00"
NSDate *updatedAt = [managedObject valueForKey:@"updatedAt"];
// ==> "2014-01-02 00:00:00 +00:00"
NSDate *publishedAt = [managedObject valueForKey:@"publishedAt"];
// ==> "2015-09-10 00:00:00 +00:00"
如果你的日期不是符合ISO 8601的规范,你也可以使用一个转换器属性来解析你的日期。首先将你的属性设置为Transformable
,然后在以下例子中,设置为DateStringTransformer
你可以在DateStringTransformer中找到日期转换器的示例。
NSDictionary *values = @{@"hobbies" : @[@"football",
@"soccer",
@"code"]};
[managedObject hyp_fillWithDictionary:values];
NSArray *hobbies = [NSKeyedUnarchiver unarchiveObjectWithData:managedObject.hobbies];
// ==> "football", "soccer", "code"
NSDictionary *values = @{@"expenses" : @{@"cake" : @12.50,
@"juice" : @0.50}};
[managedObject hyp_fillWithDictionary:values];
NSDictionary *expenses = [NSKeyedUnarchiver unarchiveObjectWithData:managedObject.expenses];
// ==> "cake" : 12.50, "juice" : 0.50
UserManagedObject *user;
[user setValue:@"John" forKey:@"firstName"];
[user setValue:@"Hyperseed" forKey:@"lastName"];
NSDictionary *userValues = [user hyp_dictionary];
就这样,你所需要做的就是将键神奇地转换成蛇形命名约定。
{
"first_name": "John",
"last_name": "Hyperseed"
}
如果你不打算导出属性/关系,你可以在排除属性的 userInfo 中添加hyper.nonExportable
禁止导出。
// TODO: 包含用户键的图片。
我们也支持关系,并遵守 Rails 的 accepts_nested_attributes_for
规则,例如对于一个有多个笔记的用户
"first_name": "John",
"last_name": "Hyperseed",
"notes_attributes": [
{
"0": {
"id": 0,
"text": "This is the text for the note A"
},
"1": {
"id": 1,
"text": "This is the text for the note B"
}
}
]
如果你不希望获取嵌套关系,你也可以忽略关系
NSDictionary *dictionary = [user hyp_dictionaryUsingRelationshipType:HYPPropertyMapperRelationshipTypeNone];
"first_name": "John",
"last_name": "Hyperseed"
或者以数组形式获取它们
NSDictionary *dictionary = [user hyp_dictionaryUsingRelationshipType:HYPPropertyMapperRelationshipTypeArray];
"first_name": "John",
"last_name": "Hyperseed",
"notes": [
{
"id": 0,
"text": "This is the text for the note A"
},
{
"id": 1,
"text": "This is the text for the note B"
}
]
NSManagedObject-HYPPropertyMapper可以通过CocoaPods使用。要安装它,只需将以下行添加到你的 Podfile 中
pod 'NSManagedObject-HYPPropertyMapper'
请查看我们的任务清单以获取有关贡献的指南。
Hyper创建了此工具。我们是一家热衷于编写高质量代码的数字通信公司,如果你正在使用这个库,我们可能希望雇佣你。
NSManagedObject-HYPPropertyMapper遵循MIT许可证。有关更多信息,请参阅LICENSE文件。