winddpan / CodableWrapper

@CodingKey("encoder", "decoder") var cool: Bool = true
MIT License
362 stars 30 forks source link

Equtable协议识别不出来 #32

Closed a594508257 closed 1 month ago

a594508257 commented 7 months ago
@Codable
struct ImproveHightligh {
    var name: String?
}

@Codable
struct ImproveSuggestion: Equatable {
    var name = ""
    let code: String?
    var highlightList: [ImproveHightligh]?

    static func == (lhs: ImproveSuggestion, rhs: ImproveSuggestion) -> Bool {
        return lhs.name == rhs.name && lhs.code == rhs.code
    }
}

这种写法会报错:Type 'ImproveSuggestion' does not conform to protocol 'Equatable'

有两种方式可以解决,我目前使用了extension的方式处理

/// 方式1,为模型ImproveHightligh添加Equtable
@Codable
struct ImproveHightligh: Equatable {
    var name: String?
}

/// 方式2,使用扩展
extension ImproveSuggestion: Equatable {
    static func == (lhs: ImproveSuggestion, rhs: ImproveSuggestion) -> Bool {
        return lhs.name == rhs.name && lhs.code == rhs.code
    }
}

推测可能是Equtable本身会对代码进行解析并自动实现func ==,Equtable解析的过程与与宏定义有冲突,可能是SwiftMacro的bug