Reputation: 2553
I have the following code fetching results from core data to populate a tableview.
private lazy var fetchedResultsController: NSFetchedResultsController = {
// Initialize Fetch Request
let fetchRequest = NSFetchRequest(entityName: "MileageLog")
// Add Sort Descriptors
let dateSort = NSSortDescriptor(key: "logDate", ascending: true)
let mileSort = NSSortDescriptor(key: "mileage", ascending: true)
fetchRequest.sortDescriptors = [dateSort, mileSort]
//// Create a new predicate that filters out any object that
//// have not been exported.
// let predicate = NSPredicate(format: "wasExported == %@", 0)
//// Set the predicate on the fetch request
// fetchRequest.predicate = predicate
let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContext = delegate.managedObjectContext
// Initialize Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()
Without the predicate the tableview is populated with all records sorted correctly. Uncommenting the two lines to bring the predicate into force returns no results at all. My dataset has 4 records with wasExported == 1 and 3 with wasExported == 0... wasExported is a Boolean but in core data it is stored as NSNumber..... what have I done wrong?
Upvotes: 0
Views: 750
Reputation: 318824
You are using the wrong format specifier in your predicate. You want:
let predicate = NSPredicate(format: "wasExported == %d", 0)
%@
is for object pointers. With %@
, the 0
is interpreted as the nil
pointer.
Upvotes: 3