斯威夫特2 - 过滤字符串数组,其中包括与QUOT;喜欢"条件 [英] Swift 2 - Filter array of strings, including "like" condition
本文介绍了斯威夫特2 - 过滤字符串数组,其中包括与QUOT;喜欢"条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我搜索过其他的答案栈,但没有匹配的斯威夫特2语法。
I searched over stack for other answers, but none matched Swift 2 Syntax.
基本上,让我们假设我的主阵列是 [你好,再见,光晕]
,和我在寻找罗
,将阵列筛选才 [你好,晕]
。
Basically, lets assume that my main array is ["Hello","Bye","Halo"]
, and i'm searching for lo
, it will filter the array only to ["Hello,Halo]
.
这是我曾尝试:
let matchingTerms = filter(catalogNames) {
$0.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch) != nil
}
会抛出
Type of expression is ambiguous without more context
有什么建议?谢谢!
推荐答案
使用 containsString
而不是:
let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { $0.containsString("lo") }
print(filtered)
对外输出
[你好,光晕]
感谢@ user3441734为指出,这些功能当然是仅当您导入基金会
Thanks to @user3441734 for pointing out that that functionality is of course only available when you import Foundation
这篇关于斯威夫特2 - 过滤字符串数组,其中包括与QUOT;喜欢"条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文