现在这是Swift编译器的奇怪行为。我在一个类中获得了一个Cannot assign to property 'self' is immutable
的编译时错误。以下是最小化的playground代码:
import UIKit
open class TextInputTraitsWrapper: NSObject {
private var wrapped: UITextInputTraits
public init(wrapped: UITextDocumentProxy) {
self.wrapped = wrapped
super.init()
}
open var keyboardAppearance: UIKeyboardAppearance {
get {
return wrapped.keyboardAppearance ?? UIKeyboardAppearance.default
}
set {
wrapped.keyboardAppearance = newValue // ERROR ON THIS LINE
}
}
}
这是我得到的错误信息(这是完整的程序):
另外,协议UITextInputTraits
这样定义keyboardAppearance
: optional var keyboardType: UIKeyboardType { get set }
。
为什么我在一个类中会得到这种类型的错误?
wrapped[keyPath: \.keyboardAppearance] = newValue
。 - Leo Dabus