目前我正在开发使用 Swift 2.0 编写的新应用程序。今天我在 Xcode beta 5
中遇到了两个奇怪的错误。如果有人使用之前的 Xcode 测试版可以确认我是否正确,我会很高兴。我也可能误解了一些东西,所以我会很感激任何反馈。
下面是一些让我纠结了一段时间的示例代码:
// Frist bug
protocol SomeProtocol {
var someArray: [String] { get set } // #1 bug
}
extension SomeProtocol {
func someFunction(someString: String) {
self.someArray.append(someString) // #1: Immutable value of type '[String]' only has mutating members named 'append'
}
}
// Second bug
protocol SomeInterfaceProtocol {
var someBool: Bool { get set } // #2 bug
}
class SomeClass: SomeInterfaceProtocol {
var someBool: Bool = false
func returnInterface() -> SomeInterfaceProtocol {
return self
}
}
let someInstance = SomeClass()
// can't set the value
someInstance.returnInterface().someBool = true // #2: Cannot assign to property: function call returns immutable value
最佳答案
如果在扩展 func 声明之前添加修饰符 mutating
就可以解决第一个错误,如下所示:
mutating func someFunction(someString: String) {
我怀疑这是语言的变化。
另一个也让我很困惑。至少,这里有一个解决方法:
var c = someInstance.returnInterface()
c.someBool = true
关于xcode - swift 2.0 测试版 : are protocols kinda broken in Xcode beta 5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039295/