0

I currently have this init method for my superclass:

- (id)initWithFoo:(int)foo;

In a subclass, I have a custom init method that calls its superclass's init, like this. Bar is exclusive to the subclass.

-(id)initWithFoo:(int)foo Bar:(int)bar {
    if (self = [super initWithFoo:foo]){
        _bar = bar;
    }
    return self;
}

I run into problems when I create an instance of the subclass, because the compiler happily suggests the superclass init method in the list of possible initialization methods for my subclass instance, which I definitely do not want.

However, if I remove initWithFoo:(int)foo from the superclass's .h file then the subclasses can no longer use it within their own init methods.

Is there any way around this?

lynk
  • 143
  • 7
  • [Related issue](http://stackoverflow.com/questions/11679885/xcode-now-generates-an-empty-category-why) (although not the same) – Yaser Jul 16 '15 at 09:04

1 Answers1

1

Yes, you can implement initWithFoo in your superclass and in your child make an "extension" declaration:

@interface SuperClass()
- (instancetype)initWithFoo:(int)foo;
@end

Make sure to place that declaration above @implementation in the .m file of your child

Yaser
  • 408
  • 2
  • 6