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)
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];
}
NSPredicate crash after swift 3 migration
The %@
format expect a Foundation object as argument, compare
"Predicate Format String Syntax" in the "Predicate Programming Guide".
You can bridge the Int64
to NSNumber
:
let id = Int64.max
let predicate = NSPredicate(format: "id == %@", id as NSNumber)
print(predicate) // id == 9223372036854775807
or change the format to "long long":
let id = Int64.max
let predicate = NSPredicate(format: "id == %lld", id)
print(predicate) // id == 9223372036854775807
Bridging all number types to NSNumber
is possible as of Swift 3.0.1 (Xcode 8.1) with the implementation of
SE-0139 Bridge Numeric Types to NSNumber and Cocoa Structs to NSValue.
Core data query value is not nil is not returning object with value equal 0
try predicate like this
myPredicate = NSPredicate(format: "rawValue != nil")
Trying to pop a UIAlert if my search predicate returns nil
Ha ha! Got it...
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text != nil) {
NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
if ([[fetchedResultsController fetchedObjects] count] != 0) {
[self.myTable reloadData];
[sBar resignFirstResponder];
}
else {
//display alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No records match."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
NSPredicate equals nil in NSFetchRequest
Make sure that your fetchRequest.predicate
value is set (i.e., is not nil).
The code in your post does show that the predicate
value is assigned. However, when the NSFetchRequest.predicate
attribute is not set then the println console message will display ((null))
for the predicate
value.
Related Topics
Merging Two Arrays of Dictionaries Based on a Shared Value
How to Send Multiple Buttons in Button.Addtarget Action? Swift3
How to Pass Text from Cell to Textview in Another View Controller
Swift Initialization Rule Confusion
Error Validating Cms Signature
Add Catextlayer on Top of Nsimageview
How to Implement Doubly Linked List in Swift
Firestore - Creating a Copy of a Collection
Swift: Convert Byte Array into Ciimage
Swiftui Textfield Resets Value and Ignores Binding
Firebase Data Structure Best Practice for User Interaction
Adding Constraints Programmatically in UIview with UItextview