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

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

问题描述

这是以下如何映射嵌套的复杂 JSON 对象并将它们保存到核心数据?.

想象一下,我的应用程序已经有了这个代码.

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
    }
}

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

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 的子类?否则它不会被持久化.我猜这也是 object 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.

推荐答案

任何自定义属性都需要可以表示为 Core Data 属性类型之一.这包括明显的东西,如字符串和数值.它还包括二进制",这是任何可以转换为/从 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.
  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天全站免登陆