0

将 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

4

1 回答 1

1

autocorrectionType 属性在 UITextInputTraits 中是可选的,但是当您在MyEditingView中显式声明它时,它不再是可选属性。我尝试在 MyEditingView 中将其设为可选属性并得到相同的编译错误。

于 2019-03-06T15:37:39.717 回答