一尘不染

过滤字符串数组,包括“ like”条件

swift

如果我["Hello","Bye","Halo"]要搜索的主数组是,"lo"它将只将该数组过滤为["Hello", "Halo"]

这是我尝试过的:

 let matchingTerms = filter(catalogNames) {
        $0.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch) !=  nil
    }

它抛出

Type of expression is ambiguous without more context

有什么建议?


阅读 235

收藏
2020-07-07

共1个答案

一尘不染

使用contains来代替:

let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { $0.contains("lo") }
print(filtered)

输出量

[“ Hello”,“ Halo”]

感谢@ user3441734指出,功能仅在您使用时才可用 import Foundation

2020-07-07