Kevin De Koninck Kevin De Koninck - 2 months ago 45
Swift Question

Swift: Store arrays of custom classes in Core Data

I'm new to Core Data but for a new project of mine, I would like to save my data to Core Data.
I want to create a Reptile-class which contains a couple of custom class arrays. Without Core Data I would have something like this:

import Foundation
import UIKit

class Reptile_ {

private var _name: String?
private var _dateOfBirth: String?
private var _morph: String?
private var _breed: String?
private var _feedingPeriodInDays: Int?
private var _reminderTime: NSDate?
private var _idealTemperatureAtDay: String?
private var _idealTemperatureAtNight: String?
private var _gender: Gender?
private var _image: UIImage?
private var _imageHeader: UIImage?

private var _sheddings: [Shedding_]?
private var _feedings: [Feeding_]?
private var _defecations: [Defecation_]?
private var _weights: [Weight_]?
private var _lengths: [Length_]?
private var _others: [Others_]?

}


And e.g. the class
Weight_
would look something like this:

import Foundation

class Weight_ {

private var _date: NSDate?
private var _weight: Double?

}


I can use Core Data to save a single class with some attributes as Strings, Boolean Data, ... But I do not know how I would save the arrays of custom objects?

I've read somewhere that I need to create Relationships (one to many) with the base
Reptile
class. So I've done that which resulted in this:

enter image description here

Is this the correct way for adding an array of custom objects? If so, How do I continue (simply click 'CreateNSManagedObject Subclass...'?)? How do I add a instance to the array? How do I read it?

Answer

You are correct that having custom arrays in Core Data consists of creating Core Data objects for those items and connecting them via relationships just as you have done in the graph you posted.

How do I continue (simply click 'CreateNSManagedObject Subclass...'?)?

Now that you have an object graph, the next step largely depends on whether you have Xcode 7 or Xcode 8. In the case of the former, you should click that create subclass button. Then, if anything changes in your data model, you'll need to regenerate the subclasses again.

In the latter (Xcode 8), however, all you need to do is look at the "Codegen" dropdown in the attributes inspector when an entity is selected in the Core Data object model file. If you select "Class Definition", Xcode 8 should generate the class for you. "Category/Extension" means it will create an extension with all of the code necessary for Core Data access, and you need to declare the actual class definition. In either case (in Xcode 8) these are updated automatically when you change the object model (currently only after rebuilding, and they won't be visible).

Image from Core Data WWDC16 session

Xcode 8

How do I read it?

Assuming you haven't set up ordering in Core Data, it will come back as an NSSet, but you may convert it to an array:

reptileInstance.lengths.allObjects as! [Length]

How do I add a instance to the array?

You can either do something simple like:

lengthInstance.reptile = reptileInstance

In which case lengthInstance will automatically be added to the lengths collection property of reptileInstance, or you can set a new NSSet to lengths on reptileInstance.

This is a very simplified explanation. You should check out the Core Data Programming Guide, but note it may or may not be yet updated for the upcoming Xcode 8.

Comments