Database Reference
In-Depth Information
Once again, Core Data has your back. If you took a close look at
SampleData.plist
, you probably noticed that each bow tie has an associated color.
Select the
Bowtie
entity in the model editor and add a new attribute named
tintColor
of data type
Transformable
:
You can save any data type to Core Data (even ones you define) using the
transformable type as long as your type conforms to the
NSCoding
protocol.
UIColor
conforms to
NSSecureCoding
, which inherits from
NSCoding
, so it can use the
transformable type out of the box. If you wanted to save your own custom object,
you would first have to implement the
NSCoding
protocol.
Note:
The
NSCoding
protocol is a simple way to archive and unarchive objects
into data buffers so they can be saved to disk.
If you want to familiarize yourself with
NSCoding
, check out Ray's
NSCoding
tutorial for a quick introduction:
http://www.raywenderlich.com/1914/nscoding-tutorial-for-ios-how-to-save-
your-app-data
Your data model is now complete. The
Bowtie
entity has the eight attributes it
needs to store all the information in
SampleData.plist
.
Managed object subclasses
In the sample project from the last chapter, you used key-value coding to access
the attributes on the
Person
entity. It looked something like this:
//Set the name
person.
setValue
(name1, forKey:
"name"
)
//Get the name
let
name = person.
valueForKey
(
"name"
)