Creating an abstract class in Objective-C

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

    Probably this kind of situations should only happen at development time, so this might work:

    - (id)myMethodWithVar:(id)var {
       NSAssert(NO, @"You most override myMethodWithVar:");
       return nil;
    }
    
    0 讨论(0)
  • 2020-11-22 16:21

    Cocoa doesn’t provide anything called abstract. We can create a class abstract which gets checked only at runtime, and at compile time this is not checked.

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

    A simple example of creating an abstract class

    // Declare a protocol
    @protocol AbcProtocol <NSObject>
    
    -(void)fnOne;
    -(void)fnTwo;
    
    @optional
    
    -(void)fnThree;
    
    @end
    
    // Abstract class
    @interface AbstractAbc : NSObject<AbcProtocol>
    
    @end
    
    @implementation AbstractAbc
    
    -(id)init{
        self = [super init];
        if (self) {
        }
        return self;
    }
    
    -(void)fnOne{
    // Code
    }
    
    -(void)fnTwo{
    // Code
    }
    
    @end
    
    // Implementation class
    @interface ImpAbc : AbstractAbc
    
    @end
    
    @implementation ImpAbc
    
    -(id)init{
        self = [super init];
        if (self) {
        }
        return self;
    }
    
    // You may override it    
    -(void)fnOne{
    // Code
    }
    // You may override it
    -(void)fnTwo{
    // Code
    }
    
    -(void)fnThree{
    // Code
    }
    
    @end
    
    0 讨论(0)
  • 2020-11-22 16:25

    Another alternative

    Just check the class in the Abstract class and Assert or Exception, whatever you fancy.

    @implementation Orange
    - (instancetype)init
    {
        self = [super init];
        NSAssert([self class] != [Orange class], @"This is an abstract class");
        if (self) {
        }
        return self;
    }
    @end
    

    This removes the necessity to override init

    0 讨论(0)
  • 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.

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

    No, there is no way to create an abstract class in Objective-C.

    You can mock an abstract class - by making the methods/ selectors call doesNotRecognizeSelector: and therefore raise an exception making the class unusable.

    For example:

    - (id)someMethod:(SomeObject*)blah
    {
         [self doesNotRecognizeSelector:_cmd];
         return nil;
    }
    

    You can also do this for init.

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