8

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 ?

1 Answer 1

19

try predicate like this

myPredicate = NSPredicate(format: "rawValue != nil")
Sign up to request clarification or add additional context in comments.

3 Comments

I did not try it because I though it was exactly the same between myPredicate = NSPredicate(format: "rawValue != nil") and myPredicate = NSPredicate(format: "rawValue != %@", "nil") ... I need to understand what is the difference between them
as per your implementation myPredicate = NSPredicate(format: "rawValue != 'nil' ")
so it is different then my proposed predicates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.