【问题标题】:Core data query value "is not nil" is not returning object with value equal 0核心数据查询值“不是零”不返回值等于 0 的对象
【发布时间】:2016-03-04 13:25:52
【问题描述】:

我尝试返回属性rawValue 不为零的对象(测量)。我的谓词是:

myPredicate = NSPredicate(format: "rawValue != %@", "nil")

还有我的要求

let fetchRequest = NSFetchRequest(entityName:"Measure")

    fetchRequest.predicate = predicate

    do {
        let fetchResults = try managedObjectContext?.executeFetchRequest(fetchRequest) as? [Measure]
        if fetchResults!.count > 0{
            measuresFetched = fetchResults!
        }
    } catch let error as NSError {
        print("Fetch failed: \(error.localizedDescription)")
    }

问题是我的查询没有返回 rawValue = 0 的对象。对我来说 0 不是 nil。

我的对象如下所示

class Measure: NSManagedObject{

    @NSManaged var rawValue: NSNumber?
    @NSManaged var date: NSDate

    class func createInManagedObjectContext(moc: NSManagedObjectContext,rawValue: Double?, date: NSDate) -> Measure {
        let newItem = NSEntityDescription.insertNewObjectForEntityForName("Measure", inManagedObjectContext: moc) as! Measure
        newItem.rawValue = rawValue
        newItem.date = date
        return newItem
    }

}

如何返回所有 rawValue 不为 nil 的对象?

【问题讨论】:

    标签: ios swift core-data


    【解决方案1】:

    试试这样的谓词

    myPredicate = NSPredicate(format: "rawValue != nil")
    

    【讨论】:

    • 我没有尝试过,因为我虽然myPredicate = NSPredicate(format: "rawValue != nil")myPredicate = NSPredicate(format: "rawValue != %@", "nil")之间完全一样......我需要了解它们之间有什么区别
    • 根据您的实施myPredicate = NSPredicate(format: "rawValue != 'nil' ")
    • 所以它和我提出的谓词不同
    猜你喜欢
    • 2021-08-12
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    相关资源
    最近更新 更多