Database Reference
In-Depth Information
Hooray! You've inserted a dog into Core Data and are currently populating the table
view with his list of walks. This list doesn't have any walks at the moment, so the
table doesn't look very exciting.
Tap the plus (+) button and it understandably does nothing. You haven't
implemented it yet! Before the transition to Core Data, this method simply added
an
NSDate
object to an array and reloaded the table view. Re-implement it as shown
below:
@IBAction
func
add(sender:
AnyObject
) {
//Insert new Walk entity into Core Data
let
walkEntity =
NSEntityDescription
.
entityForName
(
"Walk"
,
inManagedObjectContext:
managedContext
)
let
walk =
Walk
(entity: walkEntity!,
insertIntoManagedObjectContext:
managedContext
)
walk.
date
=
NSDate
()
//Insert the new Walk into the Dog's walks set
var
walks =
currentDog
.
walks
.
mutableCopy
()
as
NSMutableOrderedSet
walks.
addObject
(walk)
currentDog
.
walks
= walks.
copy
()
as
NSOrderedSet
//Save the managed object context
var
error:
NSError
?
if
!
managedContext
!.
save
(&error) {
println
(
"Could not save:
\(error)
"
)
}