Martin,

That's a nice approach.  I had, maybe mistakenly, assumed that the objects in 
the dictionary were themselves dictionaries in which case I wouldn't mind 
embedding key names in predicate strings.  The key names can be parametrized 
too, to mitigate some maintenance concerns.  I do agree that it is easy to 
abuse the -valueForKey: API including -predicateWithFormat:.

My alternate thought was to use something like...

_block BOOL itemIsPresent = NO;
[geofenceMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL* stop)
{
   if ( [[(ObjType*)obj smi] isEqual:smiObj] )
   {
       itemIsPresent = YES;
       *stop = YES;
   }
}];

// test itemIsPresent here

(written in mail...)

Because the actual key(s) are not needed this may be desirable.

Sandor Szatmari

On Jul 16, 2015, at 17:40, Martin Wierschin <mar...@nisus.com> wrote:

>>> BOOL itemIsPresentWithIdenticalValue = [[[self.geofenceObjects allValues] 
>>> filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"smi == %@", 
>>> thingWeAreCheckingFor]] count] > 0;
> ...
>> I'm not a fan of mashing everything together into one line as it makes 
>> readability and comprehension an issue, but this is exactly what I was 
>> looking for.
> 
> I agree on that point. Another thing I hate: jamming property names into 
> string literals for -valueForKey: or predicate formats. It's an easy thing to 
> miss during a refactor, and you subvert nice Xcode features like the 
> "Callers" listing.
> 
> Why not use something like:
> 
> NSSet* matches = [geofenceMap keysOfEntriesPassingTest:^BOOL(id key, id obj, 
> BOOL *stop) {
>     return [[(MyObjectType*)obj smi] isEqual:findSmi];
> }];
> BOOL isItemPresent = ([matches count] > 0);
> 
> That has the benefit of type safety, to make sure the value responds to your 
> property getter.
> 
> Also, using -keysOfEntriesPassingTest: is going to be more efficient, since 
> calling -allValues is going to create a new array just for iteration. I'm 
> assuming efficiency doesn't matter here, since otherwise your dictionary 
> would be keyed on the "smi" property, but it's a side benefit. If you were 
> concerned about efficiency in this case you could set the test block's stop 
> parameter to YES once you'd found the first match.
> 
> ~Martin Wierschin
> 

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to