Database Reference
In-Depth Information
The editor has created a class with a property for each attribute in your data model.
Note that there is a corresponding Foundation class or Swift class for every
attribute type. Here's the full mapping of attribute types to runtime classes:
•
String
maps to
String
•
Integer 16/32/64
,
Float, Double
and
Boolean
map to
NSNumber
•
Decimal
maps to
NSDecimalNumber
•
Date
maps to
NSDate
•
Binary data
maps to
NSData
•
Transformable
maps to
AnyObject
Core Data persists an
object graph
to disk, so by default it works with objects.
This is why you see primitive types like integers, doubles and Booleans boxed inside
NSNumber
. You can retrieve the actual attribute value with one of
NSNumber
's
convenience methods such as
boolValue
,
doubleValue
, and
integerValue
.
If you want to work directly with primitive data types such as
Double
and
Int32
, you
could have checked the box next to
Use scalar properties for primitive data
types
in the last dialog when you were generating the managed object subclass:
You'll focus on the object-oriented approach in this chapter, which is why you left
that box unchecked. Just be aware that there's an option out there if you don't
want to deal with
NSNumber
anymore!
Note:
Similar to
@dynamic
in Objective-C, the
@NSManaged
attribute informs the
Swift compiler that the backing store and implementation of a property will be
provided at runtime instead of at compile time.
The normal pattern is for a property to be backed by an instance variable in
memory. A property on a managed object is different: It's backed by the
managed object context, so the source of the data is not known at compile
time.
There's one more thing you need to do to before you can start using your new
managed object subclass. Open the model editor and with the
Bowtie
entity
selected, open the
Attributes
inspector. Make the following change: