class ColorizerTextFieldDelegate: NSObject, UITextFieldDelegate {
... Some protocol to colorize the text
}
class ViewController: UIViewController, UITextFieldDelegate {
... In ViewController UITextFieldDelegate is only used for character count.
}
Run Code Online (Sandbox Code Playgroud)
如果我删除 NSObject,我会收到错误,即类型 'ColorizerTextFieldDelegate' 不符合协议 'NSObjectProtocol' 但我认为 ViewController 类没有必要符合 NSObject 协议。
因为,正如您在文档中的“继承自”下所见,UIViewController继承自UIResponder,而继承自NSObject.
澄清协议与类:NSObjectProtocol和UITextFieldDelegate都是协议。UITextFieldDelegate继承自-forNSObjectProtocol协议,这意味着为了符合UITextFieldDelegate,您还需要符合NSObjectProtocol。既然UIViewController已经NSObjectProtocol因为上一段就符合了,那么它需要做的就是符合UITextFieldDelegate。ColorizerTextFieldDelegate另一方面,你的班级不是免费获得的,所以这就是为什么你需要符合NSObjectProtocol.
| 归档时间: |
|
| 查看次数: |
399 次 |
| 最近记录: |