1

Why do I need so much (seemingly useless) pass through code that objc did not require in a similar derivation:

class myNavigationController: UINavigationController {

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
...

repeat ad nauseam for every single view controller of mine loaded from xib.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • In the case above, is it necessary to override the init function if the only thing you do in your init is to call the super? – dfrib Dec 16 '15 at 11:57
  • Right. That's exactly what I'm asking: why I'm required to write an essentially a noop. To avoid some hack/kludge in swift frontend maybe? I'd rather not have to guess – Anton Tropashko Dec 16 '15 at 12:21
  • My question was if you cant just remove your override if you know that it will only contain the super.init? (And it's really a question, haven't tried it myself for this example :) ) – dfrib Dec 16 '15 at 12:22
  • (if you remove your override init(...) and just use your super's view by default, you can also remove the required init?(:NSCoder) ) – dfrib Dec 16 '15 at 12:29
  • No. You can't remove both of them. If you do that you will get something like .../myExtendedSessionNavigationController.swift:19:1: 'required' initializer 'init(coder:)' must be provided by subclass of 'UINavigationController' – Anton Tropashko Dec 16 '15 at 12:46
  • I know cause I've just tried. Have you? – Anton Tropashko Dec 16 '15 at 12:46
  • removing init(let rootViewController: UIViewController) { super.init(rootViewController:root) } along with the other two take care of this. Thank you. – Anton Tropashko Dec 16 '15 at 13:13

1 Answers1

2

This should not be an issue if your don't want to overload the designated initializer init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) your UINavigationController subclass. If you just want to make use of the default (super) initializer, you can remove both those methods from your class.

I.e., the following class

// MyNavigationController.swift
import UIKit

class MyNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // I don't want to make use of this ...
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // ... nor this
    }

    // Things I do want to do with my nav. controller
}

can be reduced to

// MyNavigationController.swift
import UIKit

class MyNavigationController: UINavigationController {

    // Things I do want to do with my nav. controller
}

Without any errors. (Verified in Swift 2.0, Xcode 7.2, simulator: iOS 9.2). This is expected behavior, see e.g. the accepted answer in thread 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'`.

If you still get an error when removing these for this subclass type, please give some details regarding your use of the class / Xcode version etc.

Community
  • 1
  • 1
dfrib
  • 70,367
  • 12
  • 127
  • 192