Protocol extension default method causing a "self is immutable" error

WPK

I am trying to extend a class-bound protocol (UITextInputTraits) with a default method:

extension UITextInputTraits where Self: UIView {

    func setTextInputTraits() {

        self.autocapitalizationType = .none // <- compiler error
    }
}

It gives a "Cannot assign to property: 'self' is immutable" error.

It works if I change the constraint from UIView to UITextField though, but that defeats the purpose of using protocols.

Why is it an error? How can I achieve implementing this default method?

Thank you!


  • cannot mark the func mutating, since 'mutating' isn't valid on methods in classes or class-bound protocols
  • tried sending messages to self, Objective-C style, but perform does not work with non-object value arguments:

    func setTextInputTraits() {
    
        let sel = #selector(setter: self.autocapitalizationType)
        self.perform(sel, with: .none)
    }
    
matt

It works if I change the constraint from UIView to UITextField though, but that defeats the purpose of using protocols. Why is it an error?

Because UIView doesn't already have an autocapitalizationType property. Thus, the compiler has no reason to believe that if it did have one, it would be settable.

How can I achieve implementing this default method?

I think you might be after something like this:

protocol MyTextInputTraits : UITextInputTraits {
    var autocapitalizationType: UITextAutocapitalizationType {get set}
}
extension MyTextInputTraits {
    func setTextInputTraits() {
       self.autocapitalizationType = .none
    }
}
extension UITextView : MyTextInputTraits {}
extension UITextField : MyTextInputTraits {}
extension UISearchBar : MyTextInputTraits {}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift 2.2 #selector in protocol extension compiler error

Swift 3 protocol extension using selector error

Swift protocol extension method is called instead of method implemented in subclass

Calling protocol default implementation from regular method

Swift protocol and extension, I need to call overridden method or default extension method as per requirement

Protocol extension method dispatch in Swift 2.0

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

django __init__ method causing argument error

Swift protocol extension method dispatch with superclass and subclass

Swift: Providing a default protocol implementation in a protocol extension

Extension of protocol with method which adds default parameter

Always the default implementation of protocol gets called even after implementing the method in class extension in an XCTest file

Error while using default value in parameter of Protocol's method

Cannot assign to property: 'self' is immutable swift error

Error: Redundant conformance of 'ViewController' to protocol with extension

vue default redirect causing maximum stack error

Puppeteer getting element from elementHandle causing protocol error

Cannot use mutating getter on immutable value: 'self' is immutable error

Script update with array method unique() causing error

Swiftui Cannot assign to property: 'self' is immutable Error

override protocol extension default implementation

Unexpected Cannot assign to property 'self' is immutable compile time error IN CLASS

Calling method is causing Error creating LLDB target

Default implementation of protocol extension in Swift not working

Implementing a default equals method for a protocol

Cast to constrained protocol can not access default method

method in protocol extension gets called instead of method implementation in View Controller

Static method is causing an unexpected indent error?

How to force usage of protocol default extension "override"?