如何使用可选方法创建协议? [英] How to create a protocol with methods that are optional?

查看:91
本文介绍了如何使用可选方法创建协议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在iPhone SDK中定义的几个协议中标记为可选的方法,例如 UIActionSheetDelegate 协议。

I noticed methods marked optional in several protocols defined in the iPhone SDK, such as the UIActionSheetDelegate protocol for example.

如何定义自己的协议,并将一些方法设置为可选?

How can I define a protocol of my own, and set a few of the methods as optional?

推荐答案

从Apple页面上的正式协议

From the Apple page on "Formal Protocols":


可选协议
方法可以使用@optional关键字标记为可选

对应于@optional模态
关键字,有一个@required关键字
来正式表示
的语义是默认行为。您可以使用
@optional和@required将
的协议划分为多个部分,因为您看到
适合。如果您未指定任何
关键字,则默认为@required。

Optional Protocol methods can be marked as optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If you do not specify any keyword, the default is @required.



@protocol MyProtocol

- (void)requiredMethod;

@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;

@required
- (void)anotherRequiredMethod;

@end

这篇关于如何使用可选方法创建协议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆