为什么ViewController类不需要在必要时为其他类符合NSObject协议

时间:2017-11-30 13:38:17

标签: ios swift

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协议。

1 个答案:

答案 0 :(得分:4)

因为,as you can see under "inherits from" in the docsUIViewController继承自UIResponder,后者继承自NSObject

澄清协议与类:NSObjectProtocolUITextFieldDelegate都是协议。 UITextFieldDelegate继承自NSObjectProtocol - 对于协议,这意味着为了符合UITextFieldDelegate,您还需要符合NSObjectProtocol 。由于上述段落UIViewController已符合NSObjectProtocol,因此需要做的就是符合UITextFieldDelegate。另一方面,您的ColorizerTextFieldDelegate课程不能免费获得,因此您需要遵守NSObjectProtocol

相关问题