How to properly subclass a delegate property in Objective-C?

后端 未结 2 1935
无人共我
无人共我 2021-02-04 13:52

In subclassing a class, I want to also subclass a delegate of the parent class given that the subclass now has additional functionality. What\'s the best way to go about doing

2条回答
  •  故里飘歌
    2021-02-04 14:19

    Follow the example of NSTableView and NSOutlineView.

    NSOutlineView is a subclass of NSTableView, and defines its own protocol for its dataSource and delegate.

    NSTableView declares its delegate this way:

    - (void)setDelegate:(id )delegate;
    - (id )delegate;
    

    and NSOutlineView:

    - (void)setDelegate:(id )anObject;
    - (id )delegate;
    

    Apparently the compiler is more lenient with bare method declarations than it is with property declarations.

    Unlike NSTable/OutlineView, you might want to make the subclass's protocol inherit from the base class's protocol, e.g.

     @protocol SpecializedProtocol 
    

    ... it probably depends on the situation.

提交回复
热议问题