Objective-C:如何查看协议?

发布于 2024-11-26 04:49:12 字数 97 浏览 1 评论 0原文

对于每个可以拥有委托的对象,都有一个相应的协议,该协议声明该对象可以向其委托发送的消息。委托为其感兴趣的事件实现协议中的方法。

如何查看协议以找出需要实现哪些功能?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸 2024-12-03 04:49:12

Objective-C 中的协议不是必需的,但它们很有用;协议通常在头文件 (.h) 中声明:

@protocol MyAwesomeProtocol

-(void)thisMethodIsRequired;

@optional

-(void)theseMethodsAreOptional;

@end

...并且通常在几个地方使用:

1:在实例变量声明中:

@class Foo : Bar
{
    id<MyAwesomeProtocol> someIvar;
}
@end

2:在属性声明中:

@class Foo : Bar
{ }
@property (assign) id<MyAwesomeProtocol> someProperty;

@end

3:在代码中(尽量避免这种情况,但是这是合法的):

if(...)
{
    [(id<MyAwesomeProtocol>)obj foo];
}

如果您使用的是 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:

@protocol MyAwesomeProtocol

-(void)thisMethodIsRequired;

@optional

-(void)theseMethodsAreOptional;

@end

... and are usually used in a couple of places:

1: In an instance variable declaration:

@class Foo : Bar
{
    id<MyAwesomeProtocol> someIvar;
}
@end

2: In property declarations:

@class Foo : Bar
{ }
@property (assign) id<MyAwesomeProtocol> someProperty;

@end

3: In code (Try to avoid this, but it's legal):

if(...)
{
    [(id<MyAwesomeProtocol>)obj foo];
}

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.

带上头具痛哭 2024-12-03 04:49:12

您可以查看文档,也可以通过在 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).

饮惑 2024-12-03 04:49:12

查看文档中的delegate属性,它几乎每次都定义为id类型以及它符合哪个协议:id.
如果没有,请阅读说明,您将找到有关该协议的更多信息。协议名称通常也是链接。

Check out the doc the delegate property, it is almost all the time defined is id 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文