如何在 Swift 中使用另一个数组中的元素过滤数组?

2024-01-24

我有两个数组

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

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

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

感谢您的帮助 :)


Use 设置操作

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Swift 中使用另一个数组中的元素过滤数组? 的相关文章

随机推荐