Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As far as I'm aware, you can't do this with a single predicate. However, you can do it with a key-path operator and a predicate in two separate calls.</p> <p>So, you have an array of arrays of terms. You want a single array of terms where its name contains 'A' (for example). Here's what you do:</p> <pre><code>NSArray * allTerms = ...; //your array of arrays of terms NSArray * collapsedTerms = [allTerms valueForKeyPath:@"@unionOfArrays.self"]; NSPredicate * filter = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", @"A"]; NSArray * filteredTerms = [collapsedTerms filteredArrayUsingPredicate:filter]; </code></pre> <p>The magic here is the <code>valueForKeyPath:</code> bit. The <a href="http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/ArrayOperators.html#//apple_ref/doc/uid/20002176-178611" rel="noreferrer"><code>@unionOfArrays</code> operator</a> takes an array of arrays of objects, and returns an array of objects. Here's what it's doing: The <code>NSArray</code> is going to break the keypath up by <code>.</code>, giving it <code>@unionOfArrays</code> and <code>self</code>. It's going to recurse and invoke <code>valueForKeyPath:</code> on itself using the second bit (self), and it's going to get back an array of arrays. It's then going to mash all those arrays together into a single array, ignoring duplicates. (If you want to remove duplicates, use <code>@distinctUnionOfArrays</code> instead)</p> <p>Once we have that array of objects, we can then filter it normally using a simple <code>name CONTAINS 'blah'</code> predicate.</p> <p>(I thought I might be able to get this to work using a <code>SUBQUERY</code>, but that's a strange beast and nothing was immediately obvious to me.)</p> <p>For the super intrepid, you could do this with a single method call using KickingBear's collection extensions (<a href="http://kickingbear.com/blog/archives/9" rel="noreferrer">available here</a>), but I would strongly advise against using hacks like that in a production setting. :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload