Creating an abstract class in Objective-C

前端 未结 21 2754
感情败类
感情败类 2020-11-22 15:44

I\'m originally a Java programmer who now works with Objective-C. I\'d like to create an abstract class, but that doesn\'t appear to be possible in Objective-C. Is this poss

21条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 16:27

    I usually just disable the init method in a class that I want to abstract:

    - (instancetype)__unavailable init; // This is an abstract class.
    

    This will generate an error at compile time whenever you call init on that class. I then use class methods for everything else.

    Objective-C has no built-in way for declaring abstract classes.

提交回复
热议问题