Creating an abstract class in Objective-C

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

    Instead of trying to create an abstract base class, consider using a protocol (similar to a Java interface). This allows you to define a set of methods, and then accept all objects that conform to the protocol and implement the methods. For example, I can define an Operation protocol, and then have a function like this:

    - (void)performOperation:(id<Operation>)op
    {
       // do something with operation
    }
    

    Where op can be any object implementing the Operation protocol.

    If you need your abstract base class to do more than simply define methods, you can create a regular Objective-C class and prevent it from being instantiated. Just override the - (id)init function and make it return nil or assert(false). It's not a very clean solution, but since Objective-C is fully dynamic, there's really no direct equivalent to an abstract base class.

    0 讨论(0)
  • 2020-11-22 16:30

    Changing a little what @redfood suggested by applying @dotToString's comment, you actually have the solution adopted by Instagram's IGListKit.

    1. Create a protocol for all the methods that make no sense to be defined in the base (abstract) class i.e. they need specific implementations in the children.
    2. Create a base (abstract) class that does not implement this protocol. You can add to this class any other methods that make sense to have a common implementation.
    3. Everywhere in your project, if a child from AbstractClass must be input to or output by some method, type it as AbstractClass<Protocol> instead.

    Because AbstractClass does not implement Protocol, the only way to have an AbstractClass<Protocol> instance is by subclassing. As AbstractClass alone can't be used anywhere in the project, it becomes abstract.

    Of course, this doesn't prevent unadvised developers from adding new methods referring simply to AbstractClass, which would end up allowing an instance of the (not anymore) abstract class.

    Real world example: IGListKit has a base class IGListSectionController which doesn't implement the protocol IGListSectionType, however every method that requires an instance of that class, actually asks for the type IGListSectionController<IGListSectionType>. Therefore there's no way to use an object of type IGListSectionController for anything useful in their framework.

    0 讨论(0)
  • 2020-11-22 16:30

    In fact, Objective-C doesn't have abstract classes, but you can use Protocols to achieve the same effect. Here is the sample:

    CustomProtocol.h

    #import <Foundation/Foundation.h>
    
    @protocol CustomProtocol <NSObject>
    @required
    - (void)methodA;
    @optional
    - (void)methodB;
    @end
    

    TestProtocol.h

    #import <Foundation/Foundation.h>
    #import "CustomProtocol.h"
    
    @interface TestProtocol : NSObject <CustomProtocol>
    
    @end
    

    TestProtocol.m

    #import "TestProtocol.h"
    
    @implementation TestProtocol
    
    - (void)methodA
    {
      NSLog(@"methodA...");
    }
    
    - (void)methodB
    {
      NSLog(@"methodB...");
    }
    @end
    
    0 讨论(0)
提交回复
热议问题