Swift 阵列参考上的快速滤波器

Swift 阵列参考上的快速滤波器,swift,swiftui,Swift,Swiftui,我有以下两个阵列: // Customers let customers: [Customer] = [ Customer(id: 1, firstname: "John", lastname: "Doe"), Customer(id: 2, firstname: "Jane", lastname: "Doe"), Customer(id: 3, firstname: "James&

我有以下两个阵列:

// Customers
let customers: [Customer] = [
    Customer(id: 1, firstname: "John", lastname: "Doe"),
    Customer(id: 2, firstname: "Jane", lastname: "Doe"),
    Customer(id: 3, firstname: "James", lastname: "Doe")
]

// Subscriptions
let subscriptions: [Subscription] = [
    Subscription(id: 1, costs: 1000, subTemplate: subscriptionTemplates[0], owner: customers[0]),
    Subscription(id: 2, costs: 700, subTemplate: subscriptionTemplates[1], owner: customers[0]),
    Subscription(id: 3, costs: 1200, subTemplate: subscriptionTemplates[6], owner: customers[1])
]
现在,我想列出所有拥有活动订阅的客户

我尝试了以下方法:

// List with all active subscriptions
List {
    ForEach(customers) { customer in
        ForEach(subscriptions) { sub in
            if(sub.owner == customer) {
                NavigationLink(destination: CustomerAboDetailView()) {
                    AboListItemView(customer: customer)
                }
            }
        }
                        
    }
}
我得到以下错误:

Binary operator '==' cannot be applied to two 'Customer' operands

有人知道解决这个问题的方法吗?

您可以使您的客户模型符合Equalable,并且可以检查两个对象是否相等

class Customer {
    var id : Int
    var firstname : String
    var lastname : String
    
    init(id: Int, firstname: String, lastname : String) {
        self.id = id
        self.firstname = firstname
        self.lastname = lastname
    }
}

extension Customer: Equatable {
    static func == (lhs: Customer, rhs: Customer) -> Bool {
        return
            lhs.id == rhs.id //<< predicate when two objects are same
    }
}
class客户{
变量id:Int
var firstname:String
var lastname:String
init(id:Int,firstname:String,lastname:String){
self.id=id
self.firstname=firstname
self.lastname=lastname
}
}
扩展客户:equalable{
静态函数==(左:客户,右:客户)->Bool{
返回

lhs.id==rhs.id/您可以使您的客户模型符合equalable,并且可以检查两个对象是否相等

class Customer {
    var id : Int
    var firstname : String
    var lastname : String
    
    init(id: Int, firstname: String, lastname : String) {
        self.id = id
        self.firstname = firstname
        self.lastname = lastname
    }
}

extension Customer: Equatable {
    static func == (lhs: Customer, rhs: Customer) -> Bool {
        return
            lhs.id == rhs.id //<< predicate when two objects are same
    }
}
class客户{
变量id:Int
var firstname:String
var lastname:String
init(id:Int,firstname:String,lastname:String){
self.id=id
self.firstname=firstname
self.lastname=lastname
}
}
扩展客户:equalable{
静态函数==(左:客户,右:客户)->Bool{
返回

lhs.id==rhs.id//为客户实施此选项是否回答您的问题?为客户实施此选项是否回答您的问题?