Core Data Predicate Not Working

CoreData fetch request NSPredicate not working

There appears to be a problem in CoreData with attribute names beginning with “is...”. Try changing the name for your attribute.

Predicate doesn't work at CoreData - Swift 4

The attribute type and the predicate type don't match.

Transformable as type for an index is nonsensical anyway. Set the attribute type to Int32, or if there are only 0 and 1 indexes even to Bool.

NSPredicate not working on Swift (CoreData)

In predicates, %@ expects an object. It's not a general "replace this" indicator, and there are different specifiers for different data types. If you're matching primitive types you need something else. For integers, try %d. For other things, look up printf-style format specifiers.

Core Data NSPredicate not working

The problem is %@ format for value:Int , cast it to value as NSObject or AnyObject like following:

NSPredicate(format: "%K == %@", colummName, value as NSObject)

Unable to fetch from coreData using Predicate as String?

When the %@ is replaced with a string variable when instantiating a NSPredicate the string variable is automatically surrounded by quotes so in your example the predicate becomes role == 'GK'

If you want to use String(format:...) you need to add the quotes yourself but in my opinion it's better to use NSPredicate(format:...) directly instead to avoid issues like this.

Core data Fetch request error for predicate

The problem is because of *%@* here %@ between the * is not replaced by a string argument so NSPredicate is not able to make your query LIKE *apple* and LIKE *macbook* what you need to do is make a search string with wildcards and with Like simply use %@, also as @Vadian suggested use number instead of Bool.

let searchkeyword1 = "apple"
let searchkeyword2 = "macbook"

//Make search string wildcards charaters
let searchString1 = "*\(searchkeyword1)*"
let searchString2 = "*\(searchkeyword2)*"

fetchRequest.predicate = NSPredicate(format: "rec_name LIKE[cd] %@ AND rec_name LIKE[cd] %@ AND isActive = %@", searchString1,searchString2, true as NSNumber)

Also batter option here is you can use CONTAINS and no need to use of wildcards.

fetchRequest.predicate = NSPredicate(format: "rec_name CONTAINS[cd] %@ AND rec_name CONTAINS[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)

You can do one more thing if you want to find all record start with apple and end with macbook then you can simply use BEGINSWITH and ENDSWITH

fetchRequest.predicate = NSPredicate(format: "rec_name BEGINSWITH[cd] %@ AND rec_name ENDSWITH[cd] %@ AND isActive = %@", "apple","macbook", true as NSNumber)


Related Topics



Leave a reply



Submit