How to determine if one array contains all elements of another array in Swift?

后端 未结 12 1155
梦如初夏
梦如初夏 2020-12-13 17:47

I have 2 arrays:

var list:Array = [1,2,3,4,5]
var findList:Array = [1,3,5]

I want to determine if list A

12条回答
  •  醉酒成梦
    2020-12-13 18:18

    In Swift 3 or Swift 4 you can write this:

    extension Array where Element: Equatable {
        func contains(array: [Element]) -> Bool {
            for item in array {
                if !self.contains(item) { return false }
            }
            return true
        }
    }
    

    You can see the contains method here

    This is just a simple extension that check if the array that you give is in the current array (self)

提交回复
热议问题