Swift字符串过滤器数组 [英] Swift filter array of strings

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

问题描述

我在快速过滤关键字(字符串)数组时遇到麻烦,我的代码:

I've had troubles filtering array of keywords (strings) in swift ,My code:

self.filteredKeywords=filter(keywords.allValues, {(keyword:NSString) ->                                              
  Bool in
    let words=keyword as? NSString
    return words?.containsString(searchText)
  })

由于AnyObject不能是NSString的子类型,所以我被困住了!

As AnyObject can't be subtype of NSString, I'm stuck with this!

推荐答案

Swift 4.2提供了一种新的方法:

Swift 4.2 provides a new way to do this:

var theBigLebowski = ["The Dude", "Angry Walter", "Maude Lebowski", "Donny Kerabatsos", "The Big Lebowski", "Little Larry Sellers"]

// after removeAll -> ["The Dude", "Angry Walter", "Donny Kerabatsos", "Little Larry Sellers"]
theBigLebowski.removeAll{ $0.contains("Lebowski")}
print(theBigLebowski)

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

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