I would know how retrieve an index of an NSArray
using a NSPredicate
?
NSArray *array = [NSArray arrayWithObjects:
@"New-York City",
@"Washington DC",
@"Los Angeles",
@"Detroit",
nil];
Wich kind of method should I use in order to get the index of "Los Angles" by giving only a NSString
NB: @"Los An"
or @"geles"
should return the same index..
Found an alternative approach helpful where the search is more complex as it allows predicate to be used to find object then object to find index:
Based on @Louie answer, instead of using for loop i had used enumeration block which worked for me.
I did this :-
You should be able to do this with blocks. Below is a snippet (I don't have a compiler handy so pls excuse any typos):
Essentially you can use the indexOfObjectPassingTest: method. This takes a block (code following the "^") and returns the index for the first object that matches your predicate (or NSNotFound if no match exists). The block iterates through each object in the array until either a match is found (at which point it returns the index) or no match is found (at which point it returns NSNotFound). Here is a link to block programming that can help you understand the logic within the block:
https://developer.apple.com/library/ios/featuredarticles/Short_Practical_Guide_Blocks/
Using NSPredicate you can get array of strings that contain your search string (it seems there's no built-in method to get just element indexes):
You can get only indexes using
indexesOfObjectsPassingTest:
method:If you want to get just one element containing your string you can use similar
indexOfObjectPassingTest:
method for that.I would do this..