gpt4 book ai didi

swift - 类协议(protocol) : Cannot use mutating member on immutable value: 'self' is immutable

转载 作者:行者123 更新时间:2023-11-28 10:52:33 44 4
gpt4 key购买 nike

我有来自框架的协议(protocol)

public protocol BaseMappable {
/// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
mutating func mapping(map: Map)
}

我有我的扩展协议(protocol)(默认实现)

public protocol DtoResponseRequestActionable: class {
func transferToResponceAction(_ dto: TransferToResponce)
}

extension DtoResponseRequestActionable where Self: BaseMappable {

func transferToResponceAction(_ dto: TransferToResponce) {
var map = Map(mappingType: .toJSON, JSON: [:])
dto.transfering(map: map)
let json = map.JSON
map = Map(mappingType: .fromJSON, JSON: json)
mapping(map: map) // <<< ERROR
}

}

但是我有错误

Cannot use mutating member on immutable value: 'self' is immutable

对于默认 imp,我该如何修复它。如果我将代码从扩展复制/粘贴到每个类,效果很好,但我需要默认 imp。任何解决方案?

最佳答案

这可能有帮助

public protocol BaseMappable : class {
/// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
func mapping(map: Map)
}

另一种方法是从

中删除类
public protocol DtoResponseRequestActionable {
mutating func transferToResponceAction(_ dto: TransferToResponce)
}

并将变异添加到

mutating func transferToResponceAction(_ dto: TransferToResponce) {

关于swift - 类协议(protocol) : Cannot use mutating member on immutable value: 'self' is immutable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45426974/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com