Database Reference
In-Depth Information
FilterViewControllerDelegate
{
Adding
FilterViewControllerDelegate
to the class declaration tells the compiler
that this class will conform to this protocol. You haven't implemented the delegate
method yet, so Xcode will complain until you do. You'll fix this in a second.
Next, add the following delegate method at the bottom of the class:
//MARK - FilterViewControllerDelegate methods
func
filterViewController(filter:
FilterViewController
,
didSelectPredicate predicate:
NSPredicate
?,
sortDescriptor:
NSSortDescriptor
?) {
fetchRequest
.
predicate
=
nil
fetchRequest
.
sortDescriptors
=
nil
if
let
fetchPredicate = predicate {
fetchRequest
.
predicate
= fetchPredicate
}
if
let
sr = sortDescriptor {
fetchRequest
.
sortDescriptors
= [sr]
}
fetchAndReload
()
}
This delegate method fires every time the user selects a new filter/sort
combination. Here, you reset your fetch request's
predicate
and
sortDescriptors
,
then unwrap the predicate and sort descriptor passed into the method and reload.
There's one more thing you need to do before you can test your price category
filters. Head over to
prepareForSegue
and add the following line inside the
if
statement:
//add line below filterVC.coreDataStack = coreDataStack
filterVC.
delegate
=
self
This formally declares
ViewController
as
FilterViewController
's delegate.
Now build and run the sample project. Go to the Filter screen, tap the first price
category cell (
$
) and then tap
Search
in the top-right corner.
What happened? Your app crashes with the following error message in the console: