Database Reference
In-Depth Information
let
request =
NSFetchRequest
(entityName:
"Bowtie"
)
let
ties =
managedObjectContext
!.
executeFetchRequest
(request,
error:
nil
)
as
[
Bowtie
]
let
sample = ties[
0
]
println
(
"Name:
\(sample.
name
)
, Worn:
\(sample.
lastWorn
)
"
)
return
true
}
On app launch, this test creates a bow tie and sets its
name
and
lastWorn
properties
before saving the managed object context.
Immediately after that, it fetches all
Bowtie
entities and prints the name and the
last worn date of the first one to the console (there should only be one).
Note:
This code doesn't do any error checking because it's meant to be a
quick and easy test. When you're writing a real application, always check for
errors. It makes the difference between a good app and a great app!
Build and run the application and pay close attention to the console:
If you've been following along carefully, the name and last worn date print to the
console as expected. This means you were able to save and fetch a
Bowtie
managed object subclass successfully. With this new knowledge under your belt, it's
time to implement the entire sample app.
Propagating a managed context
Go to
ViewController.swift
and import Core Data below where you import UIKit:
import
CoreData
Now add the following below the last
@IBOutlet
property
: