如何在Swift中使用另一个数组中的元素过滤数组? [英] How To Filter Array Using Element in Another Array in Swift?

查看:416
本文介绍了如何在Swift中使用另一个数组中的元素过滤数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组

let toBeFiltered = ["star0", "star2", "star1", "star0", "star3", "star4"]
let theFilter = ["star1", "star3"]

如何使用第二个数组过滤第一个数组?实际上theFilter可以动态更改,例如

How to filter the first array using the second array? Actually the theFilter can be changed dynamically, e.g,

let theFilter = ["star2"]
or maybe
let theFilter = ["star0", "star4", "star2"]

感谢您的帮助:)

推荐答案

使用设置操作

Set(toBeFiltered).intersection(Set(theFilter))

更多信息: https://developer.apple .com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html

这篇关于如何在Swift中使用另一个数组中的元素过滤数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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