过滤字符串数组,包括“like"状况 [英] Filter array of strings, including "like" condition

查看:13
本文介绍了过滤字符串数组,包括“like"状况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的主数组是 ["Hello","Bye","Halo"],并且我正在搜索 "lo",它将过滤数组仅用于["Hello", "Halo"].

If 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

有什么建议吗?

推荐答案

使用 contains 代替:

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

输出

["你好", "光环"]

["Hello", "Halo"]

感谢@user3441734 指出该功能当然只有在您import Foundation

Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation

这篇关于过滤字符串数组,包括“like"状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆