You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已遵循Equatable仍报错:无法将'=='应用于'any Hashable & Identifiable'操作数

问题:Equatable协议识别异常导致自定义==方法报错

问题描述

定义的NavigationDestination结构体中,object属性明确声明遵循Equatable & Hashable & Identifiable协议,但Xcode仅将其识别为Hashable & Identifiable,导致在自定义==方法中执行lhs.object == rhs.object时触发报错:Binary operator '==' cannot be applied to two 'any Hashable & Identifiable' operands

相关代码

struct NavigationDestination: Hashable, Equatable, Identifiable {
    
    let object: any Equatable & Hashable & Identifiable
    let context: [String:Any]
    
    var id: String {
        return self.object.id as! String
    }
    
    static func == (lhs: NavigationDestination, rhs: NavigationDestination) -> Bool {
        return lhs.object == rhs.object
    }
    
    public func hash(into hasher: inout Hasher) {
        return hasher.combine(id)
    }
        
}

报错截图

报错截图

内容的提问来源于stack exchange,提问作者Peter Lapisu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 12:35:28