【发布时间】:2016-09-13 21:37:49
【问题描述】:
我在为一组结果配置动态映射时遇到问题。我想要映射的结果对象嵌入在描述封闭对象类型的“容器”中。
我有类似于以下的 JSON:
"list": {
"name": ""
"item_infos": [
{
"type": "TaskItem",
"item": {
"id": 0
"title": "A Task Item",
"task": "..."
}
"url": "..."
},
{
"type": "ReminderItem",
"item": {
"id": 0
"title": "A Reminder Item",
"reminder": "..."
}
"url": "..."
}]
}
我想将其映射为具有Items 数组的List 对象,其中每个项目可能属于不同类型。不同的types 是有限且已知的。
class List: NSManagedObject {
@NSManaged var name: String
@NSManaged var items: NSOrderedSet
}
class Item: NSManagedObject {
@NSManaged var identifier: Int32
@NSManaged var title: String
// Each mappable `Item` is responsible for providing its own
// mapping object. It's overridden in the subclasses below.
class func responseMappingForManagedObjectStore(dos: RKManagedObjectStore) -> RKEntityMapping { ... }
}
class TaskItem: Item {
@NSManaged var task: String
}
class ReminderItem: Item {
@NSManaged var reminder: String
}
如何使用RKDynamicMapping 直接在列表下映射嵌入的item,同时仍然使用type 字段?我正在尝试按照这些方式进行响应映射:
let listMapping = RKEntityMapping(forEntityForName: "List", inManagedObjectStore: store)
responseMapping.addAttributeMappingsFromDictionary(["name": "title"])
let dynamicItemMapping = RKDynamicMapping()
dynamicItemMapping.setObjectMappingForRepresentationBlock { representation in
let itemMapping: RKEntityMapping
switch representation.valueForKeyPath("type") as? String {
case .Some("TaskItem"): itemMapping = TaskItem.responseMappingForManagedObjectStore(objectStore)
case .Some("ReminderItem"): itemMapping = ReminderItem.responseMappingForManagedObjectStore(objectStore)
default: return nil
// This is the bit I'm failing to solve. How can I return a mapping
// that essentially "skips" a level of the JSON, and just maps the
// embedded `item`, not the `item_info`.
let itemInfoMapping = RKObjectMapping(forClass: NSMutableDictionary.self)
itemInfoMapping.addRelationshipMappingWithSourceKeyPath("item", mapping: itemMapping)
return itemInfoMapping
}
listMapping.addPropertyMapping(RKRelationshipMapping(
fromKeyPath: "item_infos",
toKeyPath: "items",
withMapping: dynamicItemMapping))
使用此映射,会引发异常:
-[__NSDictionaryM _isKindOfEntity:]: unrecognized selector sent to instance 0x7fd2603cb400
这并不让我感到惊讶,因为动态映射的设置方式无论如何都感觉不对 - 我正在寻找一种方法来“跳过” JSON 的一个级别并且只映射嵌入的 @987654331 @。
另一种尝试是将fromKeyPath 完全指定为"item_infos.item" 与listMapping 的关系,但是我不能使用动态映射块中的type 字段来确定要使用的Item 映射的类型:
// ...
dynamicItemMapping.setObjectMappingForRepresentationBlock { representation in
// `type` inaccessible from the nested item representation
switch representation.valueForKeyPath("type") as? String {
case .Some("TaskItem"): return TaskItem.responseMappingForManagedObjectStore(objectStore)
case .Some("ReminderItem"): return ReminderItem.responseMappingForManagedObjectStore(objectStore)
default: return nil
}
listMapping.addPropertyMapping(RKRelationshipMapping(
fromKeyPath: "item_infos.item",
toKeyPath: "items",
withMapping: dynamicItemMapping))
【问题讨论】:
-
你还没有说问题出在哪里。我个人不会有多个不同的实体,我会有 1 个带有类型标识符的实体。
-
@Wain 抱歉,我没有把问题说得很清楚。我已经更新了这个问题,但简而言之,我给出的映射示例不起作用——第一个引发异常,第二个尝试基于无法从动态映射块中的字典(由于关系映射的
fromKeyPath级别太深)。为了问题的清晰/简洁起见,简化了实体;实际上,对象的不同足以保证它们是独立的实体。
标签: ios dynamic nested mapping restkit