且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用"包含"使用对象的两个数组

更新时间:2023-02-15 19:47:55

有关包含()函数工作产品类应该实现 Equatable 的协议。那是唯一的方式,我们可以检查两个元素是否相等。

For contains() function to work the Products class should implement Equatable protocol. That's the way only we can check whether two elements are equal.

class Products : Equatable {

    var name:String = ""
    var number:Int = 0

    init(name: String, number: Int) {
        self.name = name
        self.number = number
    }
}

func ==(lhs: Products, rhs: Products) -> Bool
{
    return lhs.name == rhs.name && lhs.number == rhs.number
}