Re: NSPredicate or Collection operators?
Re: NSPredicate or Collection operators?
- Subject: Re: NSPredicate or Collection operators?
- From: Martin Wierschin <email@hidden>
- Date: Thu, 16 Jul 2015 14:40:10 -0700
>> 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 (email@hidden)
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:
This email sent to email@hidden