Swift: Store arrays of custom classes in Core Data

I am new to Core Data, but for my new project I would like to save my data in Core Data. I want to create a Reptile class that contains a pair 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, for example, the Weight_ class will look something like this:

 import Foundation class Weight_ { private var _date: NSDate? private var _weight: Double? } 

I can use Core Data to save one class with some attributes in the form of strings, boolean data ... But I don’t know how to save arrays of custom objects?

I read somewhere that I need to create a relationship (one for many) with the base class Reptile . So I did what led to this:

enter image description here

Is this the right way to add an array of custom objects? If so, how do I proceed (just click "CreateNSManagedObject Subclass ..."?)? How to add an instance to an array? How to read it?

+7
arrays swift core-data
source share
1 answer

You are right that having custom arrays in Core Data consists of creating Core Data objects for these elements and connecting them through relationships just like you did in the chart you published.

How do I proceed (just click "CreateNSManagedObject Subclass ..."?)?

Now that you have an object graph, the next step depends a lot on whether you have Xcode 7 or Xcode 8. In the first case, you have to click the subclass button. Then, if something changes in your data model, you will need to restore the subclasses again.

In the latter (Xcode 8), all you have to do is look at the "Codegen" drop-down menu in the attribute inspector when the object is selected in the Core Data object model file. If you select Class Definition, Xcode 8 should generate a class for you. “Category / Extension” means that it will create an extension with all the code needed to access the master data, and you need to declare the actual definition of the class. In any case (in Xcode 8) they are updated automatically when the object model changes (currently only after the rebuild, and they will not be visible).

Image of WWDC16 Session Master Data

Xcode 8

How to read it?

Assuming you haven't set the order in Core Data, it will return as an NSSet , but you can convert it to an array:

 reptileInstance.lengths.allObjects as! [Length] 

How to add an instance to an array?

You can do something simple:

 lengthInstance.reptile = reptileInstance 

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

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

+3
source share

All Articles