NSNumber的谓词= 0

时间:2016-05-16 20:12:16

标签: ios core-data nspredicate

我有以下代码从核心数据中提取结果以填充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

}()

如果没有谓词,则会使用正确排序的所有记录填充tableview。取消注释两行以使谓词生效将不会返回任何结果。我的数据集有4条记录,wasExported == 1和3,wasExported == 0 ... wasExported是一个布尔值,但在核心数据中它存储为NSNumber .....我做错了什么?

1 个答案:

答案 0 :(得分:3)

您在谓词中使用了错误的格式说明符。你想要:

let predicate = NSPredicate(format: "wasExported == %d", 0)

%@用于对象指针。使用%@时,0将被解释为nil指针。