• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSPredicate or Collection operators?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: NSPredicate or Collection operators?
      • From: Sandor Szatmari <email@hidden>
References: 
 >NSPredicate or Collection operators? (From: Alex Zavatone <email@hidden>)
 >Re: NSPredicate or Collection operators? (From: Alex Zavatone <email@hidden>)
 >Re: NSPredicate or Collection operators? (From: Sandor Szatmari <email@hidden>)
 >Re: NSPredicate or Collection operators? (From: Alex Zavatone <email@hidden>)

  • Prev by Date: Integrating touch Id with sign up-in process in client-server based app
  • Next by Date: Re: C Sharp?
  • Previous by thread: Re: NSPredicate or Collection operators?
  • Next by thread: Re: NSPredicate or Collection operators?
  • Index(es):
    • Date
    • Thread