Protocol inheritance in Objective C

前端 未结 2 627
暖寄归人
暖寄归人 2020-12-16 04:51

I\'ve got a project which has in it a protocol, a class implementing that protocol, and a subclass of the implementation class. This is our production application.

<
相关标签:
2条回答
  • 2020-12-16 05:25

    There is no official language definition for Objective-C, but according to Apple:

    When a class adopts a protocol, it must implement the required methods the protocol declares, as mentioned earlier. In addition, it must conform to any protocols the adopted protocol incorporates. If an incorporated protocol incorporates still other protocols, the class must also conform to them. A class can conform to an incorporated protocol using either of these techniques:

    • Implementing the methods the protocol declares
    • Inheriting from a class that adopts the protocol and implements the methods

    However, reports are that GCC doesn't recognize that the property is in an incorporated protocol and is implemented in a superclass. You could change your compiler to Clang, which is reported to handle this in the specified manner, or you could just use @dynamic to tell the compiler that an implementation of the property will be provided at run time (in this case, by inheritance from the superclass).

    0 讨论(0)
  • 2020-12-16 05:42

    [XCode 3.2]

    The compiler bug is in implementation of protocol inheritance. In the example when compiling POC there are two paths to ProductionProtocol:

    1. POC -> Production -> BaseProduction -> ProductionProtocol
    2. POC -> POCProtocol -> ProductionProtocol

    This confuses GCC 4.2, it doesn't confuse Clang (LLVM 1.6, XCode 3.2).

    If you change POCProtocol to:

    @protocol POCProtocol//<ProductionProtocol>
    

    the error will go away.

    You could just comment out the protocol-to-protocol inheritance and leave a TODO to remove when the compiler is fixed.

    Looks like a classic compiler writer bug to me, or maybe someone confused with .NET semantics (where you can re-implement interfaces (protocols in Obj-C) along the inheritance chain).

    @danyowdee suggests you file a bug report, though as it is not in Clang (I didn't fire up my mothballed XCode 4 to test its compilers) I suspect this is unlikely to be a high priority to get fixed...

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