Database Reference
In-Depth Information
Now that you've finished making changes to the model, you need to create an
NSManagedObject
subclass for the new
EmployeePicture
entity. This subclass will let
you access the new entity from code.
Right-click on the
EmployeeDirectory
group folder and select
New File
.
Select the
Cocoa Touch Class
template and click
Next
. Name the
class
EmployeePicture
and make it a subclass of
NSManagedObject
. Make sure
that
Swift
is selected for the
Language,
click
Next
and finally click
Create
.
Select
EmployeePicture.swift
from the project navigator and replace the
automatically generated code with the following code:
import
Foundation
import
CoreData
class
EmployeePicture:
NSManagedObject
{
@NSManaged
var
picture:
NSData
@NSManaged
var
employee:
EmployeeDirectory
.
Employee
}
This is a very simple class with just two properties. The first,
picture
, matches the
single attribute on the
EmployeePicture
entity you just created in the visual data
model editor. The second property,
employee
, matches the relationship you created
on the
EmployeePict
u
re
entity.
Note:
You could also have Xcode create the
EmployeePicture
class
automatically. To add a new class this way, go to
Editor\Create
NSManagedObject Subclass…
, select the data model and then the
EmployeePicture
entity in the next two dialog boxes. Select
Swift
as the
language option in the final box. If you're asked, say
No
to creating an
Objective-C bridging header. Click
Create
to save the file.
Next, select the
Employee.swift
file from the project navigator and update the
code to make use of the new
pictureThumbnail
attribute and
picture
relationship.
Rename the
picture
variable to
pictureThumbnail
and add a new variable named
picture
that is of type
EmployeeDirectory.EmployeePicture
. Your file will now look
like this:
import
Foundation
import
CoreData
public
class
Employee:
NSManagedObject
{
@NSManaged
var
startDate:
NSDate
@NSManaged
var
about:
String
@NSManaged
var
active:
NSNumber
@NSManaged
var
address:
String