1

I have an Objective-C class and here is its init():

- (id)init
{
    self = [super initWithNibName:@"xxxViewController" bundle:nil];
    if (self)
    {

    }
    return self;
}

I have a Swift class that is a subclass of this. How do I override the init of the superclass in the Swift class? I would like to move the initWithNibName into the Swift class.

Here is the init in the Swift class that I have tried:

init() {
    super.init()
}

I get the error:

Must call a designated intializer of the super class

rmaddy
  • 314,917
  • 42
  • 532
  • 579
dreadbot
  • 942
  • 2
  • 11
  • 28

1 Answers1

0

Since init() is not a designated initializer (in this case, initWithNibName:bundle: is), we need to let the compiler know that it is in fact a designated initializer. We can do that by appending an attribute to the declaration in the header (after the last attribute, but before the semicolon):

@interface MyClass : Superclass
- (instancetype)init NS_DESIGNATED_INITIALIZER;
@end
Chris Zielinski
  • 1,231
  • 1
  • 9
  • 11