简体   繁体   English

iOS核心数据可转换

[英]iOS Core Data transformable

I am relatively new to iOS CoreData implementation. 我是iOS CoreData实现的新手。 Here is my problem: 这是我的问题:

I have an entity which has an 'assets' field that may contain one or many different types of objects. 我有一个实体,其中有一个“资产”字段,其中可能包含一种或多种不同类型的对象。 It is in a sub-object of another. 它在另一个的子对象中。 What is the best way to store this and how do I retrieve it. 存储此内容的最佳方法是什么,以及如何检索它。

My pseudo code example: 我的伪代码示例:

extension Group (
    @NSManaged public var created: NSDate?
    @NSManaged public var type: String?
    @NSManaged public var people: NSSet?
}

extension Person {
    @NSManaged public var created: NSDate?
    @NSManaged public var type: String?
    @NSManaged public var asset: NSObject?  //marked as transformable in data model
}

In the Person.asset it can be one or a combination of several different class of objects: 在Person.asset中,它可以是一种或几种不同类别的对象的组合:

extension Car {..}
extension House { ...} 
etc. 

My current method of saving is creating the individual type of asset and add to the person; 我当前的保存方法是创建个人资产类型并添加到个人。

let myGroup = Group(...)
let person1 = Person(...)
let hisCar = Car(..) 
person1.asset = hisCar
myGroup.addPersonObject(person: person1)

let person2 = Person(...)
let hisHouse = House(...) 
person2.asset = hisHouse
myGroup.addPersonObject(person: person2)

BEFORE I save context the following seems to work and I can access the Person objects by: 在保存上下文之前,以下方法似乎起作用,并且可以通过以下方式访问Person对象:

let peopleInGroup = Group.people!
for case let person as Person in peopleInGroup {
    if person.type != "" {
        print (person.type as! String)
    }
}

Until I save the context and Reload the application. 直到我保存上下文并重新加载应用程序。 On load of the application I fetch Group object and then try to execute the same code above. 在加载应用程序时,我获取了Group对象,然后尝试执行上面的相同代码。 It appears to be pointing at the right person object, but fails on the person.type line and I end up wit an error: [error] error: exception handling request: , -[Asset initWithCoder:]: unrecognized selector sent to instance 0x60c00027e040 with userInfo of (null) 它似乎指向正确的人对象,但在person.type行上失败,并且出现错误:[错误]错误:异常处理请求:-[Asset initWithCoder:]:无法识别的选择器发送到实例0x60c00027e040的userInfo为(null)

I believe I might need to do some sort of encoding on the asset field when I am saving and fetching? 我相信在保存和提取数据时可能需要对资产字段进行某种编码? The error is referencing the asset even though I am trying to access the type property on the person. 即使我尝试访问此人的type属性,该错误仍在引用资产。 Thanks in advance for any help. 在此先感谢您的帮助。

Transformable attributes work by using NSCoding on the attribute value. 可转换属性通过对属性值使用NSCoding来工作。 It doesn't really matter if asset has multiple types as long as all of them conform to NSCoding . asset是否具有多种类型并不重要,只要它们都符合NSCoding If adding that to your various types is a problem, you'll have to find some other way to save the asset info. 如果将其添加到您的各种类型中是一个问题,则必须找到其他方法来保存资产信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM