【问题标题】:NSPredicate to filter array of words to array of lettersNSPredicate 将单词数组过滤为字母数组
【发布时间】:2010-07-20 16:54:28
【问题描述】:

我正在尝试在创建单词时制作“加权”字母列表。我在 NSArray 中有大量单词。例如,我正在尝试获取一个新的 NSArray,其中仅根据输入的前两个字母填充所有单词的第三个字母。

到目前为止我...

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

这很适合将整个单词放入过滤后的数组中,但这并不是我所需要的。谁能告诉我一种只用wordDictionary中随机NSString的第一个、第二个或第三个字母填充filteredArray的方法吗?

编辑:澄清了我的问题。

【问题讨论】:

    标签: objective-c cocoa nsarray filtering nspredicate


    【解决方案1】:

    NSPredicate 不是您想要使用的。 NSPredicate 只是根据一个或多个标准评估对象并返回是/否结果,因此它不能用于实际操作正在测试的项目的事情。

    要获取数组中每个字符串的第三个字母并将结果放入新数组中,如下所示:

    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
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多