1

I'm looking for an explanation of why the following prints false when run on iOS. The view controller is instantiated from a dead-simple Storyboard which just contains a single table view.

I'm using the preloading trick suggested by https://stackoverflow.com/a/33658528/1919412 to allow Interface Builder to find the MyTableViewController class. I'm sure that has something to do with the problem, but I'd like to better understand what's going on.

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    // If we don't create this dummy instance before our real view controller gets loaded from storyboard, we get this error:
    //  "Unknown class _TtC21ViewControllerInitBug21MyTableViewController in Interface Builder file."
    // See: https://stackoverflow.com/a/33658528/1919412
    let foo = MyTableViewController()

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        return true
    }
}

class Foo<T>:UITableViewController {
    init() {
        super.init(style:.Plain)
    }
}

class MyTableViewController:Foo<String> {
    private var foo = true

    override func viewDidLoad() {
        super.viewDidLoad()
        print(self.foo)
    }
}
Community
  • 1
  • 1
phu
  • 1,199
  • 2
  • 10
  • 20
  • On further reflection, I don't understand why this even compiles! Shouldn't the compiler complain that I haven't implemented `init?(coder:NSCoder)`? – phu Dec 27 '15 at 19:43
  • Possible duplicate of [Generic controller in swift 2.0 using storyboards](http://stackoverflow.com/questions/32836343/generic-controller-in-swift-2-0-using-storyboards) – GetSwifty Sep 23 '16 at 19:45
  • No, that is not the same question. I even referenced that exact question in mine, specifically to prevent people from jumping to that conclusion. – phu Sep 24 '16 at 00:29

0 Answers0