Git Product home page Git Product logo

Comments (6)

hmelder avatar hmelder commented on September 24, 2024 1

Does anyone know how to fix this? I took a brief look but having a little trouble following the NSPredicate implementation.

Fixed in #273

from libs-base.

lcampbel avatar lcampbel commented on September 24, 2024

The following patch fixes this bug (and doesn't cause any of the existing NSPredicate tests to fail), but I suspect it may just be a bandaid for a specific case of a more general problem.

--- NSPredicate.m.orig	2022-01-29 14:18:23.249998976 +0000
+++ NSPredicate.m	2022-01-29 15:34:39.745337585 +0000
@@ -1049,6 +1049,8 @@
 	    e = [rightResult objectEnumerator];
 	    while ((value = [e nextObject]))
 	      {
+		if ([value isKindOfClass:[GSConstantValueExpression class]])
+		  value = [value constantValue];
 		if ([value isEqual: leftResult]) 
 		  return YES;		
 	      }

from libs-base.

rfm avatar rfm commented on September 24, 2024

I've never used NSPredicate, but looking through the code it seems to me the issue might be with the GSConstantValueExpression class used to represent the array.
I wonder if its -expressionValueWithObject:context: method should, in the case of an array, return a new array formed by calling that method on each of its elements. What do you think?

from libs-base.

lcampbel avatar lcampbel commented on September 24, 2024

Yes, the following patch does seem more general, fixes my use case, and doesn't break the existing NSPredicate tests:

--- NSPredicate.m.orig	2022-01-30 10:27:07.177865334 -0500
+++ NSPredicate.m	2022-01-30 10:36:17.211753217 -0500
@@ -1337,7 +1337,15 @@
 - (id) expressionValueWithObject: (id)object
 			 context: (NSMutableDictionary *)context
 {
+  if ([_obj isKindOfClass:[NSArray class]]) {
+    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:[_obj count]];
+    for (id o in _obj) {
+      [tmp addObject:[o expressionValueWithObject:o context:context]];
+    }
+    return tmp;
+  } else {
   return _obj;
+  }
 }

from libs-base.

rfm avatar rfm commented on September 24, 2024

I wrote/added a portable (not dependent on objc-2) version of that solution.

from libs-base.

triplef avatar triplef commented on September 24, 2024

This fix doesn’t work when the array is given as an argument instead of inside the format string like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self IN %@", @[@"yes"]];
NSArray *filteredElements = [@[@"yes", @"no"] filteredArrayUsingPredicate:predicate];
NSLog(@"Elements: %@", filteredElements);

Running the above crashes in line 1351 here, because e will be an NSString ("yes") instead of an NSExpression:

libs-base/Source/NSPredicate.m

Lines 1348 to 1354 in f334462

for (index = 0; index < count; index++)
{
NSExpression *e = [tmp objectAtIndex: index];
id o = [e expressionValueWithObject: e context: context];
[tmp replaceObjectAtIndex: index withObject: o];
}

Does anyone know how to fix this? I took a brief look but having a little trouble following the NSPredicate implementation.

from libs-base.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.