1

I encounter strange compile error when accessing UITextView as object conforming to UITextInputTraits:

protocol MyEditingView:UITextInputTraits where Self: UIView {
}

extension UITextView:MyEditingView {
}

class SomeClass {
  var editingView:MyEditingView = UITextView()
  func someFunc() {
    editingView.autocorrectionType = .no
  }
}

"Cannot assign to property: 'self' is immutable"

But if property is explicitly declared in protocol, without inheriting from UITextInputTraits it is compiled successfully.

protocol MyEditingView where Self: UIView {
  var autocorrectionType: UITextAutocorrectionType { get set }
}

And property declaration is same as in UITextInputTraits.

Swift 4.2, XCode 10.1

Vladimir
  • 7,670
  • 8
  • 28
  • 42
  • Are you aware that `UITextView` already conforms to `UITextInputTraits`? There is no need for your protocol or extension. – rmaddy Mar 06 '19 at 15:23
  • @maddy, yes, but as you can see, we speak about different protocol which in real code contains more properties. They are simply not included here because they are not related to question. – Vladimir Mar 07 '19 at 20:23

1 Answers1

1

autocorrectionType property is optional inside UITextInputTraits but when you declare it explicitly in MyEditingView its no more optional property. I tried making it optional property in MyEditingView and got the same compilation error.

Subash thapa
  • 47
  • 1
  • 4
  • Thank you @Subash thapa, yes it optional that cause an error. But it is still not clear why this irrelevant error is shown. – Vladimir Mar 07 '19 at 20:27