核心数据中具有自定义类型的所有属性都必须是一种关系吗? [英] Do all attributes with a custom type in core-data have to be a relationship?

查看:104
本文介绍了核心数据中具有自定义类型的所有属性都必须是一种关系吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自以下想象一下,我已经为我的应用准备了这段代码.

Imagine that I already have this code for my app.

class Passenger{
    var name: String
    var number: String
    var image : UIImage
    // init method
}
class Trip {
    let tripNumber : Int
    let passenger : Passenger

    init(tripNumber: Int, passenger: Passenger) {
        self.tripNumber = tripNumber
        self.passenger = passenger
    }
}

现在,我决定为我的应用添加持久性.我有一张行程表.我想显示出行旅客,但不需要表格直接查询旅客.这只是旅行的自定义对象/属性.每次访问旅客时,都是通过Trips.

Now I've decided to add persistence for my app. I just want to have a table of Trips. I want to show the passengers under trips, but don't need a table to query passengers directly. It's just a custom object/property of trip. Every time I access passenger it would be through Trips.

因此,有一种方法可以创建一个名为'TripEntity'的NSManagedObject新子类并存储我的乘客-无需1.为'Passenger'创建另一个NSManagedObject子类2.创建一个与Passenger和Trip之间具有反比关系的关系?简而言之,我只是希望它成为一个属性.从概念上来说,这也只是一个属性.这不是真正的关系...

So is there a way that I can create a new subclass of NSManagedObject named 'TripEntity' and store my passengers — WITHOUT 1. creating another NSManagedObject subclass for 'Passenger' 2. Creating a relationship with an inverse relationship between Passenger and Trip? Simply put I just want it to be an attribute. Conceptually to me it's also just an attribute. It's not really a relationship...

或者是一旦您使用Core-data,那么每个自定义类型都需要显式地成为NSManagedObject的子类吗?否则,它将不会持续存在.我猜这也是对象 graph 的意思.您的图形需要完整.图外的任何内容都将被忽略...

Or is that once you're using Core-data then every custom type needs to be explicitly a subclass of NSManagedObject? Otherwise it won't get persisted. I'm guessing this also what object graph means. That your graph needs to be complete. Anything outside the graph is ignored...

我之所以这样问,是因为我实际上要存储的JSON对象是巨大的,并且我正在尝试减少需要完成的工作.

I'm asking this because the JSON object that I actually want to store is gigantic and I'm trying to reduce the work needed to be done.

推荐答案

任何自定义属性都必须可以表示为核心数据"属性类型之一.其中包括显而易见的东西,例如字符串和数字值.它还包括二进制",即可以与NSData(在Swift中为Data)转换的任何东西.

Any custom property needs to be something that can be represented as one of the Core Data property types. That includes obvious things like strings and numeric values. It also includes "binary", which is anything that can be transformed to/from NSData (Data in Swift).

根据您的描述,最好的方法可能是

From your description, the best approach is probably to

  1. Passenger类采用NSCoding.
  2. 使passenger属性成为Core Data可转换"类型. (顺便说一句,这应该是一排乘客吗?您有一个相关的乘客,但是您的问题描述的是好像有一个以上的乘客).
  3. 执行#2之后,将passenger属性的"custom class"字段设置为您的班级名称-Passenger.
  1. Adopt NSCoding for your Passenger class.
  2. Make the passenger property a Core Data "transformable" type. (as an aside, should this be an array of passengers? You have a single related passenger, but your question describes it as if there's more than one).
  3. After doing #2, set the "custom class" field for the passenger property to be the name of your class-- Passenger.

如果您做这两件事,Core Data将自动调用NSCoding方法,以在您的Passenger类和可以保存在Core Data中的二进制Blob之间进行转换.

If you do these two things, Core Data will automatically invoke the NSCoding methods to convert between your Passenger class and a binary blob that can be saved in Core Data.

这篇关于核心数据中具有自定义类型的所有属性都必须是一种关系吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆