xcode - swift 2.0 测试版 : are protocols kinda broken in Xcode beta 5?

标签 xcode protocols swift2

目前我正在开发使用 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/

相关文章:

乘法继承 inits 和 deinits 的 Swift 最佳实践?

ios - 使用APN与后端进行iOS通信

ios - ios中滚动问题的NavigationController子ViewController中的PageViewController

ios - Xcode:仅当 PKI key 插入时,才能在钥匙串(keychain)中找到指定的项目

xcode - 在 XCode 中包含来自自定义框架的 header

ios - 为参数 "invalid value"提供了一个 'appIdName'?

swift - ProgressBar只显示结束

iphone - 循环中的 Free 和 Malloc - 释放内存的正确方法?

Python 高速公路 WAMP 消息传递协议(protocol)

swift - 有没有办法允许 Swift 协议(protocol)扩展访问实现所述协议(protocol)的类中的某些内容,但其他任何东西都无法访问?