类协议:不能在不可变值上使用可变成员:“self"是不可变的 [英] Class Protocol: Cannot use mutating member on immutable value: 'self' is immutable

查看:79
本文介绍了类协议:不能在不可变值上使用可变成员:“self"是不可变的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自框架的协议

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)
}

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

and i have my protocol with extension (default implementation)

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
    }

}

但是我有错误

不能在不可变值上使用可变成员:'self'是不可变的

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

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

How I can fix it, for default imp. If I copy/paste code from extension to every class, it works good, but I need default imp. Any solutions?

推荐答案

这可能会有所帮助

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) {

这篇关于类协议:不能在不可变值上使用可变成员:“self"是不可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆