开发者

What's the proper way to approach porting/rewriting this library?

I'm attempting to rewrite a Java library over in Objective-C. I'd like to rewrite it as an API so that certain methods are required to be implemented. I've begun to try to rewrite the code but I've run into a few questions.

  • Does Objective-C support abstract classes?

  • If not, should I be writing protocols, or just use my own classes? I know that you can mark methods required.

  • If I do write protocols, Would it be okay if I group all the protocols into one header file, or is it best practice not开发者_开发知识库 to?

  • If I use my own classes, is there a way to require subclassing of abstract methods?


There are compiler enforced abstract classes in Objective-C, protocols are what you are after.

I would personally break the protocols out into multiple headers for legibility.


There is no built in support for creating abstract classes, but you can do it by overriding the allocWithZone: method.

+ (id)allocWithZone:(NSZone *)zone {
    if([self class] == [MyAbstractClass class]) { // Check if it is a subclass or not
        NSLog(@"MyAbstractClass is an abstract superclass and should not be allocated directly. Use a concrete subclass instead.");
        return nil;
    }
    return [super allocWithZone:zone];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜