Learn Courses My Dashboard

Custom Initializer: Syntax Misunderstanding

I hope this is an alright place to ask this. On top of CWC, I’m also doing Apple’s App Development courses. And I’m stuck on a step in the textbook. I’ve tried searching online, but I’m not sure what’s going wrong so I’m stuck. I was hoping someone familiar with code could help me understand where I went wrong in the instructions.

Here Are The Instructions from the Textbook

“Create a custom initializer with the signature init?(coder: NSCoder, athlete: Athlete?). Assign self.athlete to your instance variable and call the super implementation. Satisfy the compilation error that this creates by using Xcode’s fix-it suggestion.”

I thought the only part of this instruction that I didn’t understand was what “instance variable” it’s referring to. I’m still a little fuzzy on what that term means.

Here Is What I’ve Done In My Code

The Problem
Every time I try to open a AthleteFormViewController, it hits the required init with the fatal error. I don’t understand why it’s using that initializer instead of my custom one.

Help?

Excerpt From
Develop in Swift Data Collections
Apple Education
‎Develop in Swift Data Collections on Apple Books
This material may be protected by copyright.

In your add athlete segue function where you create and return an athlete form view controller, Are you initializing both the coder and the athlete?

Since the init method is optional xcode doesn’t throw an error even if u don’t assign the athleteToEdit to the form’s athlete property

@IBSegueAction func editAthlete(_ coder: NSCoder, sender: Any?) -> AthleteFormViewController? {
        
        var athleteToEdit: Athlete?
        
        if let cell = sender as? UITableViewCell,
           let indexPath = tableView.indexPath(for: cell){
            athleteToEdit = athletes[indexPath.row]
        }
        else {
            athleteToEdit = nil
        }
        
        // When you return you init the athlete as well as the coder
        return AthleteFormViewController(coder: coder,athlete: athleteToEdit)
    }
``

Yes, I’m pretty sure I am. Here’s my code for the editAthlete segue. The only difference between mine and yours that I see is that I used a constant for athleteToEdit. But I tried it as a variable and as a constant and got the exact same problem. :cry:

I have a lead, but I’m not sure how to fix it. Using breakpoints, I realized that the code given to me included the segue for addAthlete. And that initializes the AthleteFormViewController using this:

Since it only has a coder parameter and not an athlete one, it’s not calling my initializer correct? I think it’s supposed to call the super implementation of the initializer. But instead it’s going to the required fatalError initializer.

Did I use incorrect syntax for calling the super initializer?

Does it hit the fatal error when you try to add the athlete or when you try to edit it?

In my add athlete segue func I pass in nil for athlete.

If I am not mistaken xcode is calling the override init method coz that’s the initializer with only coder: NSCoder as an argument, and to call your custom initializer you have to pass in both coder: NsCoder and athlete: Athlete? as the arguement

@IBSegueAction func addAthlete(_ coder: NSCoder) -> AthleteFormViewController? {
        return AthleteFormViewController(coder: coder, athlete: nil)
    }

Try passing in nil instead of nothing and see what happens

Cheers

So far, I’ve only seen it when I add an athlete. I don’t know if it will happen when I edit an athlete, because the app loads with no data obviously. And when I click the add button, it immediately crashes.

I really thought we had figured it out with calling the wrong initializer because I didn’t pass in an athlete. But when I changed my segue to this:

I now get this error when I click the add button. :man_facepalming:

I don’t know if it would help, but maybe I could post the Xcode project I’m working on here? Then you could compare with yours?

Dropbox Link to FavoriteAthlete Project

So the initialization issue is solved.

Your current issue seems to be with the storyboard.
Check if the referencing outlet to the league text field is correct, Coz I think that the Reference outlet and the iboutlet don’t match

Thank you! That fixed it! I had two reference outlets to the same textField.