开发者

NSPredicate to filter array of words to array of letters

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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜