我在使用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对象,其值存储在object属性中,我无法访问该对象。 打印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;
};
})
任何建议都将不胜感激,谢谢
答案 0 :(得分:2)
基本上,您可以在description
以外的返回代理对象上使用任何方法。这意味着不将其作为变量提供给NSLog
。
真实对象将存储到数据存储中。根据您的需要,您可能希望直接检索它们(通过获取或使用托管对象ID)。
答案 1 :(得分:1)
我不知道RestKit,但似乎对featureMapping
的4次调用都插入了一个新的RKAttributeMapping
- 这似乎没有任何意义。也许这就是您的featureType
财产出现问题的原因。