Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-[NSArray filteredArrayUsingPredicate:] should ignore NSNull values #349

Closed
triplef opened this issue Nov 16, 2023 · 4 comments
Closed

-[NSArray filteredArrayUsingPredicate:] should ignore NSNull values #349

triplef opened this issue Nov 16, 2023 · 4 comments
Labels
bug Use this tag for reporting bugs or issues that impede the software's normal functionality.

Comments

@triplef
Copy link
Member

triplef commented Nov 16, 2023

The following code runs fine on Apple platforms and filters for the second array entry, but with GNUstep it raises an exception Unable to find value for key "key" of object <null> (NSNull):

NSArray *array = @[@{@"key": @"value1"}, @{@"key": @"value2"}, NSNull.null];
NSArray *filtered = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"key == %@", @"value2"]];
NSLog(@"array: %@", array);
NSLog(@"filtered: %@", filtered);
@hmelder hmelder assigned hmelder and unassigned hmelder Nov 16, 2023
@hmelder hmelder added the bug Use this tag for reporting bugs or issues that impede the software's normal functionality. label Nov 16, 2023
@hmelder
Copy link
Contributor

hmelder commented Dec 9, 2023

Internally, we use GSKeyPathExpression to get the key using Key-Value-Coding. When following the backtrace on lldb, you end up at the super class method of evaluateWithObject: for whatever reasons.

In this case the predicate is parsed as a NSComparisonPredicate with a GSKeyPathExpression.

image

TLDR:

This boils down to how we handle KVCoding with NSNull.

Given this test program:

#import <Foundation/Foundation.h>

int main(void) {
  id obj = [[NSNull null] valueForKeyPath: @"key"];
  NSLog(@"Output: %@", obj);
  return 0;
}

The following is returned on macOS: 2023-12-09 19:32:08.434 a.out[6427:263545] Output: <null>
While on Debian with GNUstep:

image

@hmelder
Copy link
Contributor

hmelder commented Dec 9, 2023

I managed to resolve the issue by overriding the valueForUndefinedKey method in NSNull. This prevents the triggering of the default handler, which typically results in an exception.

It seems that Apple uses a similar approach in their implementation.

@hmelder
Copy link
Contributor

hmelder commented Dec 9, 2023

Fixed with #358

@hmelder hmelder closed this as completed Dec 9, 2023
@triplef
Copy link
Member Author

triplef commented Dec 10, 2023

Thank you for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Use this tag for reporting bugs or issues that impede the software's normal functionality.
Development

No branches or pull requests

2 participants