Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core data query value "is not nil" is not returning object with value equal 0

I try to return objects (Measure) where property rawValue is not nil. My predicate is:

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

And my request

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)")
    }

The problem is that my query is not returning objects with rawValue = 0. And for me 0 is not nil.

My objects looks like below

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
    }

}

How can I return all objects where rawValue is not nil ?

like image 613
elyrico Avatar asked Mar 04 '16 13:03

elyrico


1 Answers

try predicate like this

myPredicate = NSPredicate(format: "rawValue != nil")
like image 120
techloverr Avatar answered Nov 09 '22 23:11

techloverr