Objective C protocols usage

前端 未结 3 1327
太阳男子
太阳男子 2020-12-05 08:00

I have a homework question which confused me, really badly. Below is a brief explanation of a question.

Imagine you are developing an application that

3条回答
  •  死守一世寂寞
    2020-12-05 08:41

    However this is again explicitly checking object type indirectly, am I right?

    No, checking behavior is different from checking type. You can send -respondsToSelector: to any object, and if the result is YES you can send the message regardless of the object's type. You can also require that an object implement a given protocol, again without caring about its actual type:

    id foo;  // foo points to any type that implements SomeProtocol
    

    My second thought is to use something like abstract class in java.

    That could work, but it's apparently not what your assignment asked for, right? It says "...write a protocol..."

    Objective-C doesn't provide a way to explicitly make a class abstract the way Java does. You just create the class, and if you don't want it to be instantiated directly you document that somewhere.

提交回复
热议问题