Nspredicate to Compare Int32

NSPredicate compare with Integer

NSNumber is an object type. Unlike NSString, the actual value of NSNumber is not substitued when used with %@ format. You have to get the actual value using the predefined methods, like intValue which returns the integer value. And use the format substituer as %d as we are going to substitute an integer value.

The predicate should be,

predicateWithFormat:@"userID == %d", [stdUserNumber intValue]];

Swift: How do I create a predicate with an Int value?

When your data is safe or sanitized, you might try String Interpolation Swift Standard Library Reference. That would look something like this:

let thisSection = 1
let thisPredicate = NSPredicate(format: "sectionNumber == \(thisSection)")

Enum value and predicates

One way to do would be using CustomStringConvertible, when you conform to this protocol you would need to implement description method that return String type. Then you could use String.init method on it to get the string value.

Some thing like this,

enum MyEnum: Int32, CustomStringConvertible {
case A, B, C

var description: String {
return "\(rawValue)"
}
}

var predicate: NSPredicate = NSPredicate(format: "prop_status == %@",
argumentArray: [String(MyEnum.A)])

NSPredicate in swift for empty string

You can simply compare to the empty String, "". The %@ placeholder represents a String, so the crash happens because you supply an Int to the NSPredicate instead of a String.

You should also use the %K placeholder for variable names instead of appending strings.

let predicate = NSPredicate(format: " %K != %@", remoteAttributes.lineOwner, "")

If you also want to filter out nil values, you can use a compound predicate:

let predicate = NSPredicate(format: " %K != %@ AND %K != nil", remoteAttributes.lineOwner, "")

Issue with fetching Core Data managed object using NSPredicate by object's enum attribute

After struggling with this problem for many hours finally I have managed to solve it.

Here is what I have done to solve the problem in chronological order:

  1. I have changed the type of bookTypeValue from Int16 to Int32, because I could not see a format specifier for 16-bit integers in the official (archived) documentation. I have tested and I can confirm that if you use %@ here instead of correct format specifier, which is %d for signed 32-bit integers, you get the first error in the question:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x3)


  1. I have changed the NSPredicate(format:) call like so:

    fetchRequest.predicate = NSPredicate(format: "bookTypeValue == %d", bookType.rawValue)

  2. I have changed the 5th line in returnBook func like so:

     do {
    let results = try coreDataStack.managedContext.fetch(fetchRequest)
    if results.count > 0 {
    book = results.first
    return book
    }
    } catch let error as NSError {
    print("Fetch error: \(error) description: \(error.userInfo)")
    }

Why is this predicate format being turned into '= nil'

The %@ format expects a Foundation object as argument, the zero
is interpreted as nil.

You can convert the integer to NSNumber:

let filterPredicate = NSPredicate(format: "uid = %@", weightUnitFilter as NSNumber)

or use the "long int" format instead:

let filterPredicate = NSPredicate(format: "uid = %ld", weightUnitFilter)


Related Topics



Leave a reply



Submit