• 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: NSString and string contains
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString and string contains


  • Subject: Re: NSString and string contains
  • From: Chris Hanson <email@hidden>
  • Date: Sun, 2 Mar 2008 20:45:19 -0800

On Mar 2, 2008, at 12:31 PM, Tom Jones wrote:

I have an NSArray which contains String values and I want to loop though it and determine if any of those string contain words I'm looking for. I have tried but have been unsuccessful.

Predicates are great for this. They're a Foundation facility for performing queries on collections.


- (NSArray *)itemsInCollection:(NSArray *)collection containingWord: (NSString *)word {
NSPredicate *containsWordPredicate = [NSPredicate @"SELF CONTAINS[cd] %@", word];


return [collection filteredArrayUsingPredicate:containsWordPredicate];
}


This will return a new collection containing all items in the passed- in collection that themselvs contain the given word, and it will do the comparison in a case-insensitive and diacritic-insensitive fashion. (Thus if word is @"resume" then @"résumé" and @"Resume" would be considered matches.)

In general, any time you can express something as a query rather than as explicit iteration, it's a win -- whether you're talking about Cocoa or any other framework. This is because it lets the framework itself determine the best way to achieve the results, which can change over time.

  -- Chris

_______________________________________________

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


References: 
 >NSString and string contains (From: Tom Jones <email@hidden>)

  • Prev by Date: Re: Problems with ScriptingBridge and iTunes
  • Next by Date: Re: NSString and string contains
  • Previous by thread: Re: NSString and string contains
  • Next by thread: NSFontManager Initialization
  • Index(es):
    • Date
    • Thread