How to create a protocol with methods that are optional?

前端 未结 5 1572
青春惊慌失措
青春惊慌失措 2020-12-12 17:02

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

How can I defi

相关标签:
5条回答
  • 2020-12-12 17:11

    From the Apple page on "Formal Protocols":

    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
    
    0 讨论(0)
  • 2020-12-12 17:15

    Protocols act the same as abstract classes, so the @optional keyword defines those methods that are optional for implementation.

    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.

    @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
    
    0 讨论(0)
  • 2020-12-12 17:24

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

    TestProtocols.h

    @protocol TestProtocols <NSObject>
        @optional
        -(void)testMethodOptional;
    
        @required  // by default
        -(void)testMethodRequired;
    @end
    

    Implementation:

    TestClass.h

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

    TestClass.m

    #import "TestClass.h"
    @implemenation TestClass
        //optional to implement 
        -(void)testMethodOptional{
         // Your Code
        }
    
        //required to implement 
        -(void)testMethodRequired{
         // Your Code
        }
    @end
    
    0 讨论(0)
  • 2020-12-12 17:28

    Use the @optional keyword before your method declaration to make it 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
    0 讨论(0)
  • 2020-12-12 17:35

    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.

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

    0 讨论(0)
提交回复
热议问题