Note that there are some explanatory texts on larger screens.

plurals
  1. PONSArray find object or objects - best practices
    primarykey
    data
    text
    <p><strong>Solution:</strong> I have marked @BlackRider's answer as correct as it is the most versatile especially for complex comparisons however there are other very good answers and comments. I would encourage anyone with the same or similar question to review them and evaluate the best course of action for your specific situation.</p> <p>In my situation, I am actually not using BlackRider's solution in my implementation. I have elected to use my own solution (see Edit #2 below) with help from @JoshCaswell's comments as well as @voromax's suggestion of <code>indexesOfObjectsWithOptions:passingTest:</code> due to the fact that my comparisons are very simple in this situation. </p> <p>Thanks to everyone who answered and provided insight.</p> <hr> <p>I am looking for an efficient way to retrieve an object from an <code>NSArray</code> based on a property of that object (a unique identifier, in this case). In C#.NET using Linq I would do something like</p> <pre><code>MyObject obj = myList.Single(o =&gt; o.uuid == myUUID); </code></pre> <p>I am also wondering if there is an efficient way to get an array of objects matching a non-unique property. Again, with Linq it would look like</p> <pre><code>List&lt;MyObject&gt; objs = myList.Where(o =&gt; o.flag == true).ToList(); </code></pre> <p>Of course I can write loops to do this but they would not be reusable and I'm suspicious of their performance.</p> <p>Finding an object with a unique ID:</p> <pre><code>-(MyObject*)findObjectWithUUID:(NSString*)searchUUID{ for (MyObject* obj in _myArray){ if([obj.uuid isEqualToString: searchUUID]) return obj; } } </code></pre> <p>Finding an array of objects:</p> <pre><code>-(NSArray*)findObjectsWithFlag:(BOOL)f{ NSMutableArray* arr = [NSMutableArray array]; for (MyObject* obj in _myArray){ if(obj.flag == f) [arr addObject:obj]; } return arr; } </code></pre> <p><strong>-- EDIT --</strong></p> <p>Luckily in the first situation the object I am looking for has a unique identifier and I know there will only be one. I came up with a solution to implement isEqual on my object which will be invoked by <code>indexOfObject:</code></p> <pre><code>- (BOOL)isEqual:(id)object{ return [self.uuid isEqualToString: ((MyObject*)object).uuid]; } </code></pre> <p>And then create a "fake" lookup object and use that to find the real one</p> <pre><code>MyObject *lookupObject = [[MyObject alloc] init]; lookupObject.uuid = searchUUID; MyObject *actualObject = [_myArray objectAtIndex:[_myArray indexOfObject:lookupObject]]; </code></pre> <p>This is essentially the same as the for-in loop I posted above, but might be more readable &amp; be more reusable. Of course, this only works for finding one unique object and does not address the second half of my question.</p> <p><strong>-- EDIT 2 --</strong></p> <p>Checking <code>Class</code> and implementing <code>hash</code> as recommended in comments.</p> <pre><code>- (BOOL)isEqual:(id)object{ return [object isKindOfClass:[MyObject class]] &amp;&amp; [self.uuid isEqualToString: ((MyObject*)object).uuid]; } - (NSUInteger)hash{ return [self.uuid hash]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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