运算符功能'=='要求'Entry'符合'Equatable'



我有一些代码使用if,我想把它写在一行中,使其紧凑。。条目是枚举

if Session.shared.input?.entry == .confirm {
isButtonValidate = true
}

我想将其优化为

isButtonValidate = Session.shared.input?.entry == .confirm // Operator function '==' requires that 'Entry' conform to 'Equatable

怎样才能使它正确。谢谢你的帮助。

您可以尝试这种方法,特别是如果您无法访问Entry:

extension Entry: Equatable {
static func == (lhs: Entry, rhs: Entry) -> Bool {
lhs.id == rhs.id // <-- here, whatever is appropriate for you
}
}

最新更新