gpt4 book ai didi

ios - 为什么 ViewController 类不需要遵守 NSObject 协议(protocol),而其他类需要遵守

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:46 25 4
gpt4 key购买 nike

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”不符合协议(protocol)“NSObjectProtocol”的错误但是我发现 ViewController 类没有必要遵守 NSObject 协议(protocol)。

最佳答案

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

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

关于ios - 为什么 ViewController 类不需要遵守 NSObject 协议(protocol),而其他类需要遵守,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47574830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com