核心数据查询值“不是nil”不返回值等于0的对象

时间:2016-03-04 13:25:53

标签: ios swift core-data

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

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不是零。

我的对象如下所示

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不为零的所有对象?

1 个答案:

答案 0 :(得分:10)

尝试像这样的谓词

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