简体   繁体   English

如何使用可选方法创建协议?

[英]How to create a protocol with methods that are optional?

I noticed methods marked optional in several protocols defined in the iPhone SDK, such as the UIActionSheetDelegate protocol for example. 我注意到在iPhone SDK中定义的几个协议中标记为可选的方法,例如UIActionSheetDelegate协议。

How can I define a protocol of my own, and set a few of the methods as optional? 如何定义自己的协议,并将一些方法设置为可选?

From the Apple page on " Formal Protocols ": 来自“ 正式协议 ”的Apple页面:

Optional Protocol methods can be marked as optional using the @optional keyword. 可以使用@optional关键字将可选协议方法标记为可选。 Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. 对应于@optional modal关键字,有一个@required关键字来正式表示默认行为的语义。 You can use @optional and @required to partition your protocol into sections as you see fit. 您可以使用@optional和@required将协议划分为您认为合适的部分。 If you do not specify any keyword, the default is @required. 如果未指定任何关键字,则默认为@required。

@protocol MyProtocol

- (void)requiredMethod;

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

@required
- (void)anotherRequiredMethod;

@end

If a method in a protocol is marked as optional, you must check whether an object implements that method before attempting to call it. 如果协议中的方法被标记为可选,则必须在尝试调用之前检查对象是否实现该方法。

As an example, the pie chart view might test for the segment title method like this: 例如,饼图视图可能会测试段标题方法,如下所示:

NSString *thisSegmentTitle;
if ([self.dataSource respondsToSelector:@selector(titleForSegmentAtIndex:)]) {
    thisSegmentTitle = [self.dataSource titleForSegmentAtIndex:index];
}

The respondsToSelector: method uses a selector, which refers to the identifier for a method after compilation. respondsToSelector:方法使用一个选择器,它在编译后引用方法的标识符。 You can provide the correct identifier by using the @selector() directive and specifying the name of the method. 您可以使用@selector()指令并指定方法的名称来提供正确的标识符。

If the data source in this example implements the method, the title is used; 如果此示例中的数据源实现该方法,则使用标题; otherwise, the title remains nil. 否则,标题仍为零。

Protocols is set of rules. 协议是一套规则。 We can create protocols as below example: 我们可以创建协议,如下例所示:

TestProtocols.h TestProtocols.h

@protocol TestProtocols <NSObject>
    @optional
    -(void)testMethodOptional;

    @required  // by default
    -(void)testMethodRequired;
@end

Implementation: 执行:

TestClass.h TestClass.h

#import "TestProtocols.h"
@interface TestClass : NSObject  <TestProtocols>

@end

TestClass.m TestClass.m

#import "TestClass.h"
@implemenation TestClass
    //optional to implement 
    -(void)testMethodOptional{
     // Your Code
    }

    //required to implement 
    -(void)testMethodRequired{
     // Your Code
    }
@end

Use the @optional keyword before your method declaration to make it optional. 在方法声明之前使用@optional关键字使其成为可选项。 Simple as that! 就那么简单!

// myProtocol.h
@protocol myProtocol
- (void)myMandatoryMethod:(id)someArgument;
@optional
- (void)myOptionalMethod:(id)someArgument;
@end
// myClass.m
@interface myClass : someSuperClass <myProtocol>
    //...
@end

Protocols act the same as abstract classes, so the @optional keyword defines those methods that are optional for implementation. 协议与抽象类的行为相同,因此@optional关键字定义了可选的实现方法。

So, in the code, someMethod1, someMethod2 and someMethod4 are required methods (must be implemented). 因此,在代码中,someMethod1,someMethod2和someMethod4是必需的方法(必须实现)。 someMethod3 is optional - if we didn't implement this method, the compiler will not throw any warnings. someMethod3是可选的 - 如果我们没有实现此方法,编译器将不会抛出任何警告。

@protocol myPrtocol<NSObject>

-(void)someMethod1:(id)someArgument;
-(void)someMethod2:(id)someArugument;

@optional

-(void)someMethod3:(id)someArgument;

@required //by default

-(void)someMethod4:(id)someArgument;

@end

// sampleClass.m
@interface sampleClass : someSuperClass <myProtocol>
//...
@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM