Creating an abstract class in Objective-C

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

提交回复
热议问题