lab12
lab12

Reputation: 6448

Search a string with nsmutablearray contents

This is probably a quick and easy question, but how would I be able to search a string with the contents of a nsmutablearray which are strings. So I have the NSString *blah = @"djfald.ji". I have the nsmutablearray filled with different extensions and I want to search the string blah to see if any of the extensions have a match. I used to use -[NSRange rangeOfString:] but that doesn't work with arrays.

Thanks,

Kevin

Upvotes: 0

Views: 418

Answers (2)

Mike Abdullah
Mike Abdullah

Reputation: 15003

If you really are dealing with path extensions, it's probably better to approach this the other way round. Something like:

NSString *extension = [@"djfald.ji" pathExtension];
BOOL found = [extensions containsObject:extension];

Upvotes: 1

mrueg
mrueg

Reputation: 8225

Simply use a block:

NSUInteger extIndex = [extensionArray indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
  return [blah hasSuffix:obj];
}];
NSString *extension = extensionIndex == NSNotFound ? [extensionArray objectAtIndex extIndex] : nil;

Or simply loop through the array with an enumeration.

Upvotes: 1

Related Questions