Invalid predicate: nil RHS for second argument in NSPredicate format
On all current iOS and OS X platforms, the C int
is a 32-bit integer,
and that is what the %d
format expects on the variable argument list.
If you pass a 64-bit integer then reading the next variable argument
will read the extra 4 zero bytes.
The following table shows which format is for which integer type:
Format C type Swift type
-----------------------------------
%d int Int32
%ld long int Int
%lld long long int Int64
and similarly for the unsigned types.
Alternatively, convert the integer to a NSNumber
object and use
the %@
format, this works
for integers of all sizes. Example:
let value = 1234
let predicate = NSPredicate(format: "value = %@", value as NSNumber)
NSInvalidArgumentException', reason: 'Invalid predicate: nil RHS, need help figuring this out
The error message indicates that searchString
is nil
in
NSPredicate *filterPredicate = [NSPredicate
predicateWithFormat:@"(workoutName CONTAINS[cd] %@)", searchString];
If the intention is to display all objects if no search string is given, you should
just not assign a predicate to the fetch request in that case:
if ([searchString length] > 0) {
NSPredicate *filterPredicate = [NSPredicate
predicateWithFormat:@"(workoutName CONTAINS[cd] %@)", searchString];
[request setPredicate:filterPredicate];
}
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)
Generic NSPredicate
You can solve the problem using your earlier solution for having different implementations for different parameter types : overloading.
func predicate<T: CVarArg>(value: String, op: String, predicate: T) -> [NSPredicate] {
return [NSPredicate(format: "\(value) \(op) %@", predicate)]
}
func predicate(value: String, op: String, predicate: Int) -> [NSPredicate] {
return [NSPredicate(format: "\(value) \(op) %d", predicate)]
}
Hope this helps.
Good luck.
Predicate for NSNumber = 0
You are using the wrong format specifier in your predicate. You want:
let predicate = NSPredicate(format: "wasExported == %d", 0)
%@
is for object pointers. With %@
, the 0
is interpreted as the nil
pointer.
Core Data predicate : unimplemented SQL generation for predicate
"ANY" in a Core Data predicate works only for a single to-many relationship.
Since your query involves two to-many relationships, you have to use a SUBQUERY:
[NSPredicate predicateWithFormat:@"SUBQUERY(models, $m, ANY $m.trims IN %@).@count > 0",
arrayOfTrims];
Localizable.strings works for String, but not for Double and Integer 32
Use only %d
. %d
is for an integer type
"elo-rating %d" = "Elo rating: %d";
For Average score, Use %lf
.
"avg-score %lf" = "Average score: %lf";
Related Topics
Add Uitapgesturerecognizer to Uitextview Without Blocking Textview Touches
How to Hide the Home Indicator with Swiftui
How to Create a PDF File Programmatically in an iOS Application
How to Use Afnetworking or Sthttprequest to Make a Request of a Soap Web Service
Stroke Width with a Scenekit Line Primitive Type
Facebooksdk(4.1.X) Custom Login UI Button - Swift(1.2)
Nspredicate for Array of Dictionaries
Why Must a Protocol Operator Be Implemented as a Global Function
Application Identifier Entitlement Value Has Changed
Generate Rsa Public Key from Modulus and Exponent
Gamecenter iOS 9 Gamecenter Gklocalplayerlistener Methods Not Called
Does iOS Calendar Support a Url Scheme
Objective-C Rabbitmq Client Not Publishing Messages to Queue
Uicollectionviewcell Dynamic Height W/Two Dynamic Labels & Auto Layout