Creating an abstract class in Objective-C

前端 未结 21 2758
感情败类
感情败类 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:16

    The solution I came up with is:

    1. Create a protocol for everything you want in your "abstract" class
    2. Create a base class (or maybe call it abstract) that implements the protocol. For all the methods you want "abstract" implement them in the .m file, but not the .h file.
    3. Have your child class inherit from the base class AND implement the protocol.

    This way the compiler will give you a warning for any method in the protocol that isn't implemented by your child class.

    It's not as succinct as in Java, but you do get the desired compiler warning.

提交回复
热议问题