将 UITextView 作为符合 UITextInputTraits 的对象访问时遇到奇怪的编译错误:
protocol MyEditingView:UITextInputTraits where Self: UIView {
}
extension UITextView:MyEditingView {
}
class SomeClass {
var editingView:MyEditingView = UITextView()
func someFunc() {
editingView.autocorrectionType = .no
}
}
“无法分配给属性:‘自我’是不可变的”
但是,如果在协议中显式声明了属性,而不从 UITextInputTraits 继承,则它会成功编译。
protocol MyEditingView where Self: UIView {
var autocorrectionType: UITextAutocorrectionType { get set }
}
属性声明与 UITextInputTraits 中的相同。
斯威夫特 4.2,XCode 10.1