Favorite Athlete Lab - Fatal error thown when performing 'Add Athlete' SegueAction

I'm working through the Favorite Athlete Lab in the Develop in Swift Data Collections book.

The guide says:

In the addAthlete method, instantiate and return a new instance of AthleteFormViewController.

When I control-dray to create the IBActionSegue, I am given the code:

@IBSegueAction func addAthlete(_ coder: NSCoder) -> AthleteFormViewController? {
    return <#AthleteFormViewController(coder: coder)#>
  }

I'm not sure what I should add but I currently have:

@IBSegueAction func addAthlete(_ coder: NSCoder) -> AthleteFormViewController? {
    let newViewController = AthleteFormViewController(coder: coder)
    return newViewController
  }

When I run the app and click the button connected to the Segue Action, the required init on the View Controller gives the error:

Fatal error: init(coder:) has not been implemented

I dont know if the way I instantiate and return AthleteFormViewController is the issue, but it seems to be where it goes wrong and where I dont understand quite what is required.

Update: Got it working. The problem did not seem to be the way I created and returned the instance, but the auto-added required init on the target View Controller.

Xcode's fix supplied:

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

But I had to change this to:

required init?(coder: NSCoder) {
    super.init(coder: coder)
  }

What I'm trying to work out now is if I should have known this already? I did not.

What I'm trying to work out now is if I should have known this already?

Earlier in the course, we were told to use the auto-fix and just leave it at that because "we wouldn't need the default implementation of init()" again. There wasn't any other similar problem. I think the author thought we'd be able to figure it out given what we'd learned before.

Your solution works. My guess why the auto-fix provides the error message instead of the basic implementation like yours is that since you're providing custom init() implementation with an extra parameter, you might also want to override the default implementation, which the error is aimed to remind you of.

Some labs and exercises are so vague in the course. It's as if the author expected us to know what they were thinking.

Greetings,

I had the same exact issue. I fixed the issue by following these steps.

  • I found warnings for redundant/duplicate outlet connections to UITextFields of AthleteFormViewController in the Inspector Area and I deleted them as I already created my own IBOutlets for each of the textfields as per the book.

By doing the above, I was able to edit an existing Athlete cell but still was not able to add/create a new Athlete cell.

  • I did not change init?(coder: NSCoder) which Xcode suggested
  • I changed the function body of @IBSegueAction function by returning AthleteFormViewController(coder: coder, athlete: nil) instead of AthleteFormViewController(coder: coder)
@IBSegueAction func addButton(_ coder: NSCoder) -> AthleteFormViewController? {
        return AthleteFormViewController(coder: coder, athlete: nil)
}

After making these changes, I was able to run the app successfully.

Thanks for your time!

Favorite Athlete Lab - Fatal error thown when performing 'Add Athlete' SegueAction
 
 
Q