这是Swift编译器的奇怪行为。我在一个类中得到一个Cannot assign to property 'self' is immutable编译时错误。下面是最低限度的操场代码:
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 }。
为什么我在课堂上会犯这样的错误??
发布于 2021-02-06 23:45:45
此错误与您创建的类无关。它与变量“包装”的底层协议相关。不能直接修改keyboardAppearance属性。
相反,您需要在源代码中修改它。这是用户键入的textfield/搜索栏。
要做到这一点,只需访问UITextField /UISearchBar,并在那里更改keyboardAppearance属性即可。
textField.keyboardAppearance = UIKeyboardAppearance.darkhttps://stackoverflow.com/questions/66082736
复制相似问题