Objective-C:如何查看协议?
对于每个可以拥有委托的对象,都有一个相应的协议,该协议声明该对象可以向其委托发送的消息。委托为其感兴趣的事件实现协议中的方法。
如何查看协议以找出需要实现哪些功能?
For every object that can have a delegate, there is a corresponding protocol, that declares the messages that the object can send it's delegates. The delegate implements methods from the protocol for events it is interested in.
How can one view the protocol in order to find out what functionality needs to be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Objective-C 中的协议不是必需的,但它们很有用;协议通常在头文件 (.h) 中声明:
...并且通常在几个地方使用:
1:在实例变量声明中:
2:在属性声明中:
3:在代码中(尽量避免这种情况,但是这是合法的):
如果您使用的是 Xcode,您始终可以按住 Command 键并单击出现在代码中任意位置的协议,以跳转到定义该协议的标头。即使对于 Apple 的协议也是如此,因为头文件没有被编译。此外,通过 Xcode 提供的文档还提供了有关哪些方法是必需的或可选的的更多见解。
由于您可以定义可选的协议方法,因此您应该始终检查您的委托是否 -respondsToSelector:@selector(isThisMethodImplemented:),因为该语言不会为您执行此操作。
另外,如果您使用的是 Xcode,则可以按住 Option 键单击代码中的类来调出快速文档面板,其中有一个选项可以转到您单击的对象的类的完整文档。
Protocols in Objective-C are non-essential, but they are useful; Protocols are usually declared in header (.h) files:
... and are usually used in a couple of places:
1: In an instance variable declaration:
2: In property declarations:
3: In code (Try to avoid this, but it's legal):
If you're using Xcode, you can always command-click a protocol that appears anywhere in your code to jump to the header where that protocol is defined. This is true even of Apple's protocols, since header files are not compiled. Also, the documentation available through Xcode provides additional insight on what methods are required or optional.
Since you can define optional protocol methods, you should always check to see if your delegate -respondsToSelector:@selector(isThisMethodImplemented:), since the language doesn't do this for you.
Also, if you're using Xcode, you can option-click a class in your code to bring up the quick documentation panel, which has an option to go to the full documentation for the class of the object you clicked on.
您可以查看文档,也可以通过在 Xcode 中按住 Command 键单击协议(在 Xcode 3 中按住 Command 键双击)来查看相应的头文件。
You can either look at the documentation or view the corresponding header file by Command-clicking the protocol in Xcode (Command-doubleclick in Xcode 3).
查看文档中的
delegate
属性,它几乎每次都定义为id
类型以及它符合哪个协议:id.
如果没有,请阅读说明,您将找到有关该协议的更多信息。协议名称通常也是链接。
Check out the doc the
delegate
property, it is almost all the time defined isid
type and which protocol it is conforming to :id <TheProtocolYouLookFor>
.If not, read down the description and you will find more information about the protocol. Protocol names are also links in general.