-3

What happens to be trivial task in Objective-C

- (instancetype)initWithTitle:(NSString *)title {
    return [super initWithTitle:title];
    //bool but_it_works_in_objective_c;
}

causes short-circuit errors with suggestions: only nil value permitted at return >> Failable initializer 'init(title:)' cannot override a non-failable initializer >> only nil value permitted at return ...

Screenshots with compiler errors from Xcode 9.3 (Swift 4.1)

Long came transitioning from id to returned instancetype seems to have the idea merely to distinguish lack of presence of return in init family of initializers. Why such an obvious hint could not make it way to autocorrection then?

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
3F71792A
  • 1
  • 1
  • though NSMenu has not much with the question present in general only taken as an example to most common type which happens to be useful yet not being NSView – 3F71792A Jun 17 '18 at 06:05

1 Answers1

1

In Swift a (non-failable) init method does not return anything and you must not change the signature of the designated initializer

class MyMenu : NSMenu {

    override init(title: String) {
        super.init(title: title)
    }

    required init(coder decoder: NSCoder) {
        super.init(coder: decoder)
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • yes, that is right and only the way. transitioning to returned _instancetype_ seems to have the idea merely to distinguish presence of return in `unit` family of initializer's body. Why such an obvious hint could not make it way to autocorrection? – 3F71792A Jun 17 '18 at 05:18