class ColorizerTextFieldDelegate: NSObject, UITextFieldDelegate {
... Some protocol to colorize the text
}
class ViewController: UIViewController, UITextFieldDelegate {
... In ViewController UITextFieldDelegate is only used for character count.
}
如果删除NSObject,我会收到类型'ColorizerTextFieldDelegate'不符合协议'NSObjectProtocol'的错误 但我发现ViewController类没有必要符合NSObject协议。
答案 0 :(得分:4)
因为,as you can see under "inherits from" in the docs,UIViewController
继承自UIResponder
,后者继承自NSObject
。
澄清协议与类:NSObjectProtocol
和UITextFieldDelegate
都是协议。 UITextFieldDelegate
继承自NSObjectProtocol
- 对于协议,这意味着为了符合UITextFieldDelegate
,您还需要符合NSObjectProtocol
。由于上述段落UIViewController
已符合NSObjectProtocol
,因此需要做的就是符合UITextFieldDelegate
。另一方面,您的ColorizerTextFieldDelegate
课程不能免费获得,因此您需要遵守NSObjectProtocol
。