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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/322498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:56:31  来源:igfitidea点击:

How to create a protocol with methods that are optional?

iphoneobjective-c

提问by jpm

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

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

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

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

回答by Matt Gallagher

From the Apple page on "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.

可选的协议方法可以使用@optional 关键字标记为可选。对应于@optional modal 关键字,有一个@required 关键字来正式表示默认行为的语义。您可以使用@optional 和@required 将您的协议划分为您认为合适的部分。如果不指定任何关键字,则默认为@required。

@protocol MyProtocol

- (void)requiredMethod;

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

@required
- (void)anotherRequiredMethod;

@end

回答by Zephyr

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. You can provide the correct identifier by using the @selector() directive and specifying the name of the method.

RespondsToSelector: 方法使用一个选择器,它指的是编译后的方法的标识符。您可以通过使用 @selector() 指令并指定方法的名称来提供正确的标识符。

If the data source in this example implements the method, the title is used; otherwise, the title remains nil.

如果本例中的数据源实现了该方法,则使用标题;否则,标题保持为零。

回答by Vikram Biwal

Protocols is set of rules. We can create protocols as below example:

协议是一套规则。我们可以创建如下示例的协议:

TestProtocols.h

测试协议.h

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

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

Implementation:

执行:

TestClass.h

测试类.h

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

@end

TestClass.m

测试类.m

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

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

回答by e.James

Use the @optionalkeyword before your method declaration to make it optional. Simple as that!

@optional在方法声明之前使用关键字使其成为可选的。就那么简单!

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

回答by user3540599

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). someMethod3 is optional - if we didn't implement this method, the compiler will not throw any warnings.

所以,在代码中,someMethod1、someMethod2和someMethod4是必须的方法(必须实现)。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