gpt4 book ai didi

swift 4 : "cannot use mutating member on immutable value: ' self' is immutable"in mutating function

转载 作者:行者123 更新时间:2023-11-30 11:31:45 36 4
gpt4 key购买 nike

这段代码

extension Collection {
mutating func f() {
removeFirst()
}
}

处理错误

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

我认为 mutating 关键字应该使“self”可变。为什么?

最佳答案

RangeReplaceableCollection是集合所遵循的协议(protocol)如果您可以添加或删除元素,则符合:

extension RangeReplaceableCollection {
mutating func f() {
self.removeFirst()
}
}

关于 swift 4 : "cannot use mutating member on immutable value: ' self' is immutable"in mutating function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50177701/

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