我注意到在 iPhone SDK 中定义的几个协议(protocol)中标记为可选的方法,例如 UIActionSheetDelegate
协议(protocol)。
如何定义自己的协议(protocol),并将其中一些方法设置为可选?
最佳答案
来自“Formal Protocols”上的 Apple 页面:
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
https://stackoverflow.com/questions/322498/