How to create a protocol with methods that are optional?

前端 未结 5 1606
青春惊慌失措
青春惊慌失措 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: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 
        //...
    @end

提交回复
热议问题