views:

97

answers:

1

I am trying to make "weighted" list of letters as words are created. I have a massive list of words in an NSArray. For example I am trying to acquire a new NSArray filled with just the 3rd letters of all the words based off the first two letters entered.

So far I have...

NSArray *filteredArray;
if (currentWordSize == 0) {
    filteredArray = wordDictionary
}
else {
    NSPredicate *filter = [NSPredicate predicateWithFormat:@"SELF beginswith[cd] %@", filterString];
    filteredArray = [wordDictionary filteredArrayUsingPredicate:filter];
}

And that works all good for putting whole words into the filtered array, but that isn't exactly what I need. Can anyone show me a way to just fill the filteredArray with just the 1st, 2nd, or 3rd letters of a random NSString from the wordDictionary?

EDIT: clarified my question.

A: 

NSPredicate isn't what you're going to want to use for this. NSPredicate simply evaluates objects based on one or more criteria and returns a yes/no result, so it can't be used to do things that actually manipulate the items being tested.

To grab the third letter of each string in an array and put the results in a new array would look something like this:

NSArray* wordDictionary;
NSMutableArray* filteredArray = [[NSMutableArray alloc] init];

for (NSString* aString in wordDictionary)
{
    if ([aString length] > 2)
        [filteredArray addObject:[aString substringWithRange:NSMakeRange(2, 1)]];
    else
        [filteredArray addObject:@""]; //there is no third letter
}
Brian Webster
I'd remove the else :)
willcodejavaforfood