我正在尝试通过添加一个默认方法来扩展一个类绑定的协议(
UITextInputTraits
):extension UITextInputTraits where Self: UIView {
func setTextInputTraits() {
self.autocapitalizationType = .none // <- compiler error
}
}
它会报错:"Cannot assign to property: 'self' is immutable"
。
如果我将约束从UIView
更改为UITextField
,它就可以正常工作,但这违背了使用协议的目的。
为什么会出错?如何实现此默认方法?
谢谢!
- 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) }