Snowman
Snowman

Reputation: 32061

Creating IBOutlet in Swift results in 'class is not constructible with ()'

My code compiled fine before upgrading to beta 4, however I think they changed something with IBOutlets.

The old syntax was:

@IBOutlet var tableView: UITableView

The new syntax is:

@IBOutlet weak var tableView: UITableView!

This is default code generated by Xcode when I ctrl drag from my xib file to the class file.

However, with this new syntax, I'm unable to construct an instance of my class. Take the following example:

class TestViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
}

Then if I try to do either

var controller = TestViewController(nibName: nil, bundle: nil)

or

var controller = TestViewController()

I get an error:

TestViewController is not constructible with ()

What's the right way to create an instance of my controller then? The only way that currently works for me is to make the outlets optional, but I'd rather not do that.

Upvotes: 4

Views: 415

Answers (2)

iOS_Developer
iOS_Developer

Reputation: 183

Can you try this.

@IBOutlet var tableView: UITableView?

I know this is not the answer but the error is gone after this.

Upvotes: 1

Snowman
Snowman

Reputation: 32061

The solution seems to be to implement the init method in that view controller:

init()
{
    super.init(nibName: nil, bundle: nil)
}

I'm not sure why that is.

Upvotes: 0

Related Questions