0
我没有找到正确的映射并获取很多记录。使用RKEntityMapping和RKRelationshipMapping获取许多记录
实体只有两个属性nid(Integer 16)和body(String)。
的JSON(由Drupal的web服务,只是它的一部分创建)就像是
[{
"nid":[{"value":"4"}],
"body":[{"value":"<p>test test test<\/p> \r\n","format":"basic_html","summary":""}]
}]
和我的代码
let mapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
mapping.identificationAttributes = ["nid"]
let nidMapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
nidMapping.addAttributeMappingsFromDictionary([
"value" : "nid"
])
let bodyMapping = RKEntityMapping(forEntityForName: "Content", inManagedObjectStore: manager.managedObjectStore)
bodyMapping.addAttributeMappingsFromDictionary([
"value" : "body"
])
mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "nid", toKeyPath: "nid", withMapping: nidMapping))
mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: "body", toKeyPath: "body", withMapping: bodyMapping))
let responseDescriptor = RKResponseDescriptor(
mapping: mapping,
method: RKRequestMethod.GET,
pathPattern: "",
keyPath: "",
statusCodes: NSIndexSet(index: 200)
)
我得到3个nsmanagedobjects而不是1:
nid nil, body nil
nid 0, body <p>test test test</p>
nid 4, body nil
谢谢!
显示实体。 JSON是讨厌的。 – Wain
json来自drupal。我在帖子中添加了实体的属性类型。谢谢 –
所以你有没有关系的普通值,但你正在使用关系映射?您需要一个自定义值转换器,而不是关系映射。 – Wain