目前,我正在开发使用Swift 2.0编写的新应用。今天我面对两个奇怪的错误Xcode beta 5
。如果有人使用Xcode的先前Beta版可以确认我是否正确,我将非常乐意。我也可能会误解某些内容,因此,感谢您的反馈。
以下是一些示例代码,这些代码使我有些挣扎:
// 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
如果您mutating
在扩展功能声明之前添加修饰符,则可以解决第一个错误,如下所示:
mutating func someFunction(someString: String) {
我怀疑这是语言上的变化。
另一个也让我感到困惑。至少,这是一种解决方法:
var c = someInstance.returnInterface()
c.someBool = true
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句