88722

NSPredicate for two NSNumber arrays

Question:

I have a bit of a hard time writing a predicate for my search functionality and thought you'd be bale to help. So basically I have two arrays of NSNumbers. I want my predicate to satisfy the following:

<pre class="lang-none prettyprint-override">If a number's integerValue in array A matches any integerValue in array B.

I don't want to use any sort of loop for this solution. Here's what I have so far

ANY integerValue == ANY //how do I pass the entire array here and ask for the integerValue of each member?

Answer1:

The ANY operator will handle that.

Since it is a bit difficult to say from your question which of the arrays is "self" in normal predicate parlance, I'll write it without a self:

NSArray *arrayA = @[@2, @3, @7]; NSArray *arrayB = @[@2, @4, @9]; NSPredicate *pred = [NSPredicate predicateWithFormat: @"ANY %@ IN %@", arrayA, arrayB];

Due to the lack of a "self", it will have to be evaluated with nil as the object, but that works fine:

BOOL matched = [pred evaluateWithObject: nil];

If you prefer to have a "self" in the predicate, you can just enter it:

NSPredicate *pred = [NSPredicate predicateWithFormat: @"ANY self IN %@", arrayB]; BOOL matched = [pred evaluateWithObject: arrayA];

The result is the same.

<h3>A small conceptual comment</h3>

The predicate above evaluates to true if any integer is included in both arrays, which is how I read your question.

This means that, conceptually speaking, you seem to be testing whether two sets of numbers intersect each other. NSSet's method intersectsSet: checks that, so another way to do the test would be to keep your numbers as sets and test for intersection:

matched = [setA intersectsSet: setB];

Answer2:

I know it's not precisely what you asked for (predicates and all) but another way is to use NSArray's - (id) firstObjectCommonWithArray:(NSArray *)otherArray, which would return nil if no common object can be found.

BOOL arraysIntersect = [array1 firstObjectCommonWithArray:array2] != nil;

One caveat though is that it would use its own object equality rules when comparing two objects, meaning if two objects are NSNumber instances, it will compare them using NSNumber's compare: method. But the same goes for the predicate-based solution proposed so far.

Recommend

  • How to rotate object around local axis in OpenGL?
  • Vectorized array comparison in Fortran
  • Convert subarray index to array index in MATLAB?
  • Ruby average value of array of time values (fixnums)
  • What is an instance of an NSObject
  • Backup of Data Lake Store
  • Core data how update a record?
  • NSPredicate acting strange in NSFetchedResultsController
  • Find value in Array in objective C
  • How do I assign array variable in a simple Pthread programming?
  • how to find the index position of the ARRAY Where NSPredicate pick the value. I use filteredArrayUsi
  • Is it good to have multiple database running in a same project?
  • Dojox/mvc/at model scope
  • How to concatenate two or more arrays in PHP without loosing values if it is same key and different
  • How do I debug a segmentation fault in Mono on Ubuntu without any debugger?
  • How can you Call a method from a diffrent Project, both in C++?
  • How to do nested queries in neo4j
  • Changing the WPF DatePicker Year/Month header area background color
  • How do display a UIAlertView from a block on iOS?
  • symfony render json_array entity type and save using form
  • IF statement formatting best-practise, what's your style?
  • PyYaml parses '9:00' as int
  • ASP.NET MVC - Detect Time Spent on Page
  • How does ActiveSupport do month sums?
  • NSMutableArray instance used in a block
  • How to access meteor package name inside package?
  • Saving Changes After In-App Purchase Has Been Purchased
  • Row Count Is Returning the incorrect number using RaptureXML
  • How to do unit test for HttpContext.Current.Server.MapPath
  • How to rebase a series of branches?
  • Email format validation in mvc3 view
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Azure Cloud Service Web Role web pages do not load
  • How to add date and time under each post in guestbook in google app engine
  • How to model a transition system with SPIN
  • what is the difference between the asp.net mvc application and asp.net web application
  • Arrays break string types in Julia
  • Python: how to group similar lists together in a list of lists?
  • coudnt use logback because of log4j
  • Django query for large number of relationships