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:
- I have changed the type of
bookTypeValue
fromInt16
toInt32
, 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)
I have changed the
NSPredicate(format:)
call like so:fetchRequest.predicate = NSPredicate(format: "bookTypeValue == %d", bookType.rawValue)
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
Swift Override Static Method Compile Error
Extend All Number Types in Swift
Uibutton Action Is Not Triggered After Constraint Layouts Changed
How to Draw a Line Between Two Points Over an Image in Swift 3
Weak Reference to Closure in Swift
Bridging Header for Flurry.H Not Working with Pod
Detect When a Custom Cell Is Selected from Within The Cell Itself
How to Restore In-App Purchases Correctly
Position of Mouse Click Relative to Scene, Not Window
Binary to Hexadecimal in Swift
How to Update UIviewrepresentable with Observableobject
Decoding a Nested Array in Swift 4
Which Optimization Level Should I Choose for Release
Preferredstatusbarupdateanimation Being Ignored
Type 'Bundle' Has No Member "Module"