问题描述
不知道这是怎么回事,这似乎应该很简单.我有一个可变var的协议,具有可变功能的扩展.当我尝试使用扩展中声明的 mtkAnimQueAppend 时,事情在 testClass.testFunc 中崩溃了,我收到以下错误消息:不能在不可变的成员上使用变异成员值:自我"是一成不变的.
Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc, when I try and use mtkAnimQueAppend declared in the extension, I get this error: "Cannot use mutating member on immutable value: 'self' is immutable.
protocol MTKAnimateValueDelegate {
var mtkAnimQue:[MTKAnimateValue]? {get set}
}
extension MTKAnimateValueDelegate {
///Adds element to que
mutating func mtkAnimQueAppend(element:MTKAnimateValue) {
if mtkAnimQue != nil {
mtkAnimQue?.append(element)
} else {
mtkAnimQue = [element]
}
}
}
class testClass: MTKAnimateValueDelegate {
var mtkAnimQue:[MTKAnimateValue]?
func testFunc() {
var animValue = MTKAnimateValue(fromValue: 10, toValue: 20, inSeconds: 2)
animValue.isAnimating = true
mtkAnimQueAppend(animValue) //ERROR: "Cannot use mutating member on immutable value: 'self' is immutable
}
}
推荐答案
问题是,在协议中,您将该函数标记为mutating,如果要在结构上使用该协议,则需要这样做.但是,传递给testFunc
的self是不可变的(它是对该类实例的引用),这会使编译器崩溃.如果testClass实际上是一个结构,并且可以对函数进行更改以解决该问题,那么这将是有道理的.
The problem is that, in the protocol you mark the function as mutating, which you need to do if you want to use the protocol on a struct. However, the self that is passed to testFunc
is immutable (it's a reference to a instance of the class) and that is tripping up the compiler. This would make sense if testClass was actually a struct and you could make the function mutating to resolve the issue.
我可以看到两种解决方法:
I can see two work arounds:
-
仅使协议类为
make the protocol class only
protocol MTKAnimateValueDelegate: class { ...
使testClass为一个结构,并将testFunc标记为变异.
Make testClass a struct and mark testFunc as mutating.
无论哪种方式,我认为这都是需要向Apple报告的错误.
Either way, I think this is a bug that needs to be reported to Apple.
修改
- 另一种解决方法是制作
self
的可变副本
- Another way around it is to make a mutable copy of
self
func testFunc() {
var animValue = MTKAnimateValue(fromValue: 10, toValue: 20, inSeconds: 2)
animValue.isAnimating = true
var mutableSelf = self
mutableSelf.mtkAnimQueAppend(animValue)
}
由于mutableSelf
是参考,所以变异函数所做的任何更改仍将反映在self
的状态中.
Since mutableSelf
is a reference, any changes the mutating function makes will still be reflected in self
's state.
这篇关于Swift 2错误,在协议扩展中使用了变异函数“不能在不可变值上使用变异成员:'self'是不可变的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!