Missing argument for parameter 'coder' in call

I have a RegistrationController which is a ViewController. It uses a class data structure that requires

required init?(coder aDecoder: NSCoder) {
    registrationList = RegistrationList()
    super.init(coder: aDecoder)
}

I want to call the Registration Class from appdelegate using the following code.

let registrationController = UINavigationController(rootViewController: RegistrationController())
window?.rootViewController = registrationController

I get the following error message and do not know how to get around it.

Missing argument for parameter 'coder' in call

Here, you don't seem to have init() implemented, and so compiler ask that you use init(coder:) instead.

I use this data class

class RegistrationList {

enum Status: Int,CaseIterable {
    case playing, notPlaying
}

var register: [PlayersDetails] = []

private var playing: [PlayersDetails] = []
private var notPlaying: [PlayersDetails]  = []

init () { ...

So have implemented an initiator. so not sure why it does not work. Also implemented required init?(coder aDecoder: NSCoder) {
registrationList = RegistrationList()
super.init(coder: aDecoder)
} in the view class.

Could you post the RegistrationList and RegistrationController, inits (only the signature will do).

Also, you can put code in triple tick marks ```,

```
like this
```

You posted RegistrationList here, but I suspect the error is that RegistrationController is missing init().

Here is my definition of RegistrationList.

class RegistrationList {
    
    enum Status: Int,CaseIterable {
        case playing, notPlaying
    }
    
    var register: [PlayersDetails] = []
    
    private var playing: [PlayersDetails] = []
    private var notPlaying: [PlayersDetails]  = []
    
    init () {
        let row0Item = PlayersDetails()
        row0Item.firstName = "Andy"
        row0Item.lastName = "Mason"
        row0Item.displayName = "Simon Mason"
        row0Item.email = "Simon Mason@gmail"
        row0Item.password = "Simon Mason"
        
        addPlayer(row0Item, for: .notPlaying)
       
    }
    func addPlayer(_ item: PlayersDetails, for status: Status, at index: Int = -1) {
        switch status {
        case .playing:
            if index < 0 {
                playing.append(item)
            } else {
                playing.insert(item, at: index)
            }
        case .notPlaying:
            if index < 0 {
                notPlaying.append(item)
            } else {
                notPlaying.insert(item, at: index)
            }
        }
    }
    
    func register(for status: Status) -> [PlayersDetails] {
        switch status {
        case .playing:
            return playing
        case .notPlaying:
            return notPlaying
        }
    }
    
    func move(item: PlayersDetails, from sourcePriority: Status, at sourceIndex: Int, to destinationPriority: Status, at destinationIndex: Int) {
        remove(item, from: sourcePriority, at: sourceIndex)
        addPlayer(item, for: destinationPriority, at: destinationIndex)
        
    }

Below is the init in the RegistrationController

class RegistrationController: UIViewController {

required init?(coder aDecoder: NSCoder) {
    registrationList = RegistrationList()
    super.init(coder: aDecoder)
}
    ```

RegistrationController does not seem to have an init(). It does have init?(coder:). Those are different, though - note that they do not have the exact same names.

I think what is happening is that the compiler is confused about what you meant because your code said RegistrationController() and so it was looking for init() without any other arguments but the closest one it found was init?(coder:) so that is what it is suggesting.

In short, I think you need to try adding an init() implementation to RegistrationController in order to be able to write the code you wanted to write. Alternatively, you can likely instead write:

let registrationController = UINavigationController(rootViewController: RegistrationController(nibName: nil, bundle: nil))

And that may work because the superclass, UIViewController, defines an initializer with that name.

Below is the complete code the gives the error.

'''
import UIKit

class PlayersDetails {

var firstName = ""
var lastName = ""

}
class RegistrationList {

enum Status: Int,CaseIterable {
    case playing, notPlaying
}

var register: [PlayersDetails] = []

private var playing: [PlayersDetails] = []
private var notPlaying: [PlayersDetails]  = []

init() {
    
    let row0Item = PlayersDetails()
    row0Item.firstName = "Andy"
    row0Item.lastName = "Mason"
    addPlayer(row0Item, for: .notPlaying)
    
}

func addPlayer(_ item: PlayersDetails, for status: Status, at index: Int = -1) {
    switch status {
    case .playing:
        if index < 0 {
            playing.append(item)
        } else {
            playing.insert(item, at: index)
        }
    case .notPlaying:
        if index < 0 {
            notPlaying.append(item)
        } else {
            notPlaying.insert(item, at: index)
        }
    }
}

func register(for status: Status) -> [PlayersDetails] {
    switch status {
    case .playing:
        return playing
    case .notPlaying:
        return notPlaying
    }
}

func move(item: PlayersDetails, from sourcePriority: Status, at sourceIndex: Int, to destinationPriority: Status, at destinationIndex: Int) {
    remove(item, from: sourcePriority, at: sourceIndex)
    addPlayer(item, for: destinationPriority, at: destinationIndex)
    
}

func remove(_ item: PlayersDetails, from  status: Status, at index: Int) {
    switch status {
    case .playing:
        playing.remove(at: index)
    case .notPlaying:
        notPlaying.remove(at: index)
    }
}

}

class RegistrationController: UIViewController {

var cellId = "cellId"
var registrationList: RegistrationList

lazy var registrationTable: UITableView = {
    let table = UITableView()
    table.translatesAutoresizingMaskIntoConstraints = false
    table.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    return table
}()

fileprivate func setupView() {
    
    view.addSubview(registrationTable)
        
    registrationTable.topAnchor.constraint(equalTo: view.topAnchor, constant: 10).isActive = true
    registrationTable.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    registrationTable.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
    registrationTable.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    registrationTable.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

}
override func viewDidLoad() {
    super.viewDidLoad()
    
  //  view.backgroundColor = .red
    
    registrationTable.delegate = self
    registrationTable.dataSource = self
    
    setupView()
    
}

required init?(coder aDecoder: NSCoder) {
    registrationList = RegistrationList()
    super.init(coder: aDecoder)
}

}
extension RegistrationController: UITableViewDelegate, UITableViewDataSource,UISearchBarDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   
    let cell = registrationTable.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as UITableViewCell
    cell.textLabel?.text = "hello"
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(indexPath.row)
}

}
'''
In a--delegate I have added the following
""
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow(frame:UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    
    let viewController =  UINavigationController(rootViewController: ViewController())
    window?.rootViewController = viewController
           return true
}

'''

This gives me the error.

It seems to be as @BigZaphod said, in your RegistrationController, you have init?(coder:) (with 1 argument), but when you use it, you do RegistrationController() with no argument.

The 1-argument version is the best you compiler could get, but that still doesn't match, hence the error. The suggestion is also as @BigZaphod mentioned.

PS
It's actually 3 tick marks ```, not 3 single quote ''' for the code block. Personally, it's pretty awkward to type in my keyboard, so I use text replacement to do qq -> ```.

As @BigZaphod stated, you have to add an init() method

init() {
   registrationList = RegistrationList()
}

to handle the default initialization of your sub-class property. You could also add the initialization to the declaration

var registrationList = RegistrationList()

and get rid of all the initializers. The UIViewControllers will handle the initializations it provides, and you've initialized the only property in your sub-class in the declaration, so your class and super-class are initialized. You don't have to provide an init if all it does it invoke the super-class methods only.

However, if you do provide a default initializer init(), then you have to provide the required init(...).

Thanks everyone. The change that worked was var registrationList = RegistrationList(), thanks jonprescott.

Terms of Service

Privacy Policy

Cookie Policy