我想使用NSPredicate从Core Data获取对象。我做到了,但是当我启动NSFetchRequest时,NSPredicate等于控制台中显示的nil。我有Xcode 6.4,Swift 1.2。我重新启动了Xcode,但是得到了相同的结果。我该怎么做?

func fetchRequest() -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "Car")
    let sortDescriptor = NSSortDescriptor(key: "personCar", ascending: true)
    fetchRequest.predicate = NSPredicate(format: "personRelationship.namePerson contains[c] %@", currentPerson)
    fetchRequest.fetchBatchSize = 50
    fetchRequest.sortDescriptors = [sortDescriptor]
    println(fetchRequest)
    return fetchRequest
}


来自控制台的消息

<NSFetchRequest: 0x1700db040> (entity: Car; predicate: ((null)); sortDescriptors: ((
    "(personCar, ascending, compare:)"
)); batch size: 50; type: NSManagedObjectResultType; )

最佳答案

确保设置了fetchRequest.predicate值(即不为nil)。

您帖子中的代码确实表明已分配predicate值。但是,当未设置NSFetchRequest.predicate属性时,println控制台消息将为((null))值显示predicate

关于ios - NSPredicate在NSFetchRequest中等于nil,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32329821/

10-09 13:23