XLYMapping 设计用于将 JSON 映射到本地对象。目标对象可以是继承自 NSObject 的对象,因为我使用 KVC 来设置值。
更多详细信息可以在下面的示例和测试代码中找到。
验证过程兼容某些指定的类型:数字、字符串、集合、有序集合和 URL。请阅读代码获取更多详细信息。
//definition of TestAnimal class
@interface TestAnimal : NSObject
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end
//definition of TestCat class
@interface TestCat : TestAnimal
@property (nonatomic, strong) UIColor *eyeColor;
@end
//definition of TestDog class
@interface TestDog : TestAnimal
@property (nonatomic, copy) NSURL *aboutLink;
@end
//this is a example. our JSON is:
{"dogs":[
{
"name":"DogA",
"age":5,
"about link":"http://link.to.DogA"
},
{
"name":"DogB",
"age":6,
"about link":"http://link.to.DogB"
}],
"cats":[
{
"name":"CatC",
"age":3,
"eye color":"70,70,70,1"
},
{
"name":"CatD",
"age":4,
"eye color":null
}]
}
//setup mapping.
XLYObjectMapping *dogMapping = [XLYObjectMapping mappingForClass:[TestDog class]];
[dogMapping addAttributeMappingFromArray:@[@"name", @"age"]];
[dogMapping addAttributeMappingFromDict:@{@"about link":@"aboutLink"}];
XLYObjectMapping *catMapping = [XLYObjectMapping mappingForClass:[TestCat class]];
[catMapping addAttributeMappingFromArray:@[@"name", @"age"]];
[catMapping addMappingFromKeyPath:@"eye color" toKey:@"eyeColor" construction:^id(id JSONObject) {
NSArray *colorComponents = [JSONObject componentsSeparatedByString:@","];
return [UIColor colorWithRed:[colorComponents[0] floatValue] / 255.0f
green:[colorComponents[1] floatValue] / 255.0f
blue:[colorComponents[2] floatValue] / 255.0f
alpha:[colorComponents[3] floatValue]];
}];
XLYMapping *mapping = [XLYObjectMapping mappingForClass:[NSDictionary class]];
[mapping addRelationShipMapping:dogMapping fromKeyPath:@"dogs" toKey:@"dogs"];
[mapping addRelationShipMapping:catMapping fromKeyPath:@"cats" toKey:@"cats"];
//perform transform
NSDictionary *result = [mapping performSyncMappingWithJSONObject:JSONObject error:&error];
//you will get a dictionary which has two keys of "dogs" and "cats". see test codes for more detail.
//definition of TestPerson class
@interface TestPerson : NSManagedObject
@property (nonatomic, assign) int32_t identity;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) int32_t age;
@property (nonatomic, strong) NSSet *cars;
@end
//definition of TestCar class
@interface TestCar : NSManagedObject
@property (nonatomic, copy) NSString *vendor;
@property (nonatomic, assign) int64_t identity;
@property (nonatomic, strong) TestPerson *person;
@end
[
{
"name":"kaizei",
"age":26,
"id":1,
"cars":[
{
"vendor":"lamborghini",
"id":1001
},
{
"vendor":"porsche",
"id":1002
}
]
},
{
"name":"yimi",
"age":25,
"id":2,
"cars":[
{
"vendor":"cadillac",
"id":1003
},
{
"vendor":"mercedes-benz",
"id":1004
}
]
}
]
//create mappings.
XLYManagedObjectMapping *carMapping = [XLYManagedObjectMapping mappingForClass:TestCar.class
entityName:@"Car"
primaryKeys:@[@"identity"]
managedObjectContext:self.context];
[carMapping addAttributeMappingFromDict:@{@"id":@"identity"}];
[carMapping addAttributeMappingFromArray:@[@"vendor"]];
XLYManagedObjectMapping *personMapping = [XLYManagedObjectMapping mappingForClass:TestPerson.class
entityName:@"Person"
primaryKeys:@[@"identity"]
managedObjectContext:self.context];
[personMapping addAttributeMappingFromDict:@{@"id":@"identity"}];
[personMapping addAttributeMappingFromArray:@[@"name", @"age"]];
[personMapping addRelationShipMapping:carMapping fromKeyPath:@"cars" toKey:@"cars"];
//transform json
NSArray *result = [self.mapping performSyncMappingWithJSONObject:self.JSONObject error:&error];
//you will get an array of two persons which are ManagedObject in 'self.context'.
//here we reuse the people mapping and child mapping.
XLYObjectMapping *theMapping = [XLYObjectMapping mappingForClass:nil];
theMapping.dynamicMappingBlock = ^XLYObjectMapping *(id JSONObject) {
if (JSONObject[@"child_name"]) {
return childMapping;
}
return peopleMapping;
};
您也可以为缺失的值设置默认值。如果系统在某个映射中遇到 nil,则将使用默认值,否则忽略。您还可以使用 willMapBlock 检查 JSON 是否缺失某些内容,只需检查它,如果缺失某些内容,添加一些值。
设置默认值的方式很简单
[childMapping setDefaultValueForAttributes:@{@"isMale" : @YES}];
注意:字典中的键是 toKey。
您还可以将一个管理的对象映射(ManagedObjectMapping)作为一个正常NSObject映射的关系映射。有关更多详细信息,请参阅 testManagedObjectMapping_embedded
测试方法。
请注意,并非所有不同种类的映射都可以添加为关系映射。这取决于具体实现。例如,您只能将 managedObjectMapping 添加为 managedObjectMapping 的关系。
如果您想在 Swift 中使用此映射系统,您必须
因为我们的系统使用 KVC,但 Swift 中的 KVC 支持不如 Objective-C 强。您不能对可选类型使用 'setValueForKey'。