我在使用 RestKit 0.20.3 映射没有关键路径的 JSON 字符串数组时遇到问题。我的课程基于 RestKit wiki中的示例。
JSON:
"feature_list":{
"property":["test","test ","test","test"],
"street":["test1","foo","bar"],
"garden":["foo","bar"],
"other":["foo","bar", "test2"]
}
课程:
@interface FeatureList : NSManagedObject
@property(nonatomic, strong) NSSet *property;
@property(nonatomic, strong) NSSet *street;
@property(nonatomic, strong) NSSet *garden;
@property(nonatomic, strong) NSSet *other;
@end
@interface Feature : NSManagedObject
@property(nonatomic, strong) NSString *featureType;
@end
映射设置:
+ (RKMapping *)featureListMapping {
RKObjectMapping *mapping = [RKEntityMapping mappingForEntityForName:@"FeatureList" inManagedObjectStore:objectStore];
[mapping addRelationshipMappingWithSourceKeyPath:@"property" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"street" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"garden" mapping:[self featureMapping]];
[mapping addRelationshipMappingWithSourceKeyPath:@"other" mapping:[self featureMapping]];
}
+ (RKMapping *)featureMapping {
RKObjectMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Feature" inManagedObjectStore:objectStore];
[mapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"featureType"]];
return mapping;
}
在 Xcode 中调试 feature.featureType 对象时,返回一个 RKMappingSourceObject 对象,其值存储在我无法访问的对象属性中。打印 feature.featureType.class 时,打印 NSCFString。
for (Feature * feature in featureList.property)
{
//feature.featureType is a RKMappingSourceObject in the debugger
NSString *featureStr = feature.featureType.class; //prints NSCFString
}
日志输出:
Mapped attribute value from keyPath '(null)' to 'featureType'. Value: test ({
HTTP = {
request = {
URL = "http://localhost:3000/api/v1/test";
headers = {
};
method = GET;
};
response = {
URL = "http://localhost:3000/api/v1/test";
headers = {
"Cache-Control" = "max-age=0, private, must-revalidate";
"Content-Type" = "application/json; charset=utf-8";
Etag = "\"193d0f470155872e5b36e9f7586c0c8f\"";
"Proxy-Connection" = Close;
Server = "thin 1.5.0 codename Knife";
"X-Request-Id" = 0e4de78e656ef2165699385695cdfe75;
"X-Runtime" = "0.092788";
"X-UA-Compatible" = "IE=Edge";
};
};
};
mapping = {
collectionIndex = 1;
rootKeyPath = response;
};
})
任何建议将不胜感激,谢谢