Warm tip: This article is reproduced from serverfault.com, please click

Missing argument for parameter in call

发布于 2017-04-26 23:35:55

New to swift (3) and Xcode (8) and I'm using firebase to load some data in a tableView . When I try to build the app, I get the error: "Missing argument for parameter name in call" in my fetchWhiskey function on the line when I call an instance of WhiskeyItem. I can't figure out why this error is happening. can anyone help me out?

Here's my class:

import UIKit
class WhiskeyItem {
    let wName: String
    let wType: String
    
    init(wName: String, wType: String) {
        self.wName = wName
        self.wType = wType
    }
}

and here's the tableView that I'm trying to load the data in:

import UIKit
import Firebase
import FirebaseDatabase

class FirstViewTableViewController: UITableViewController, UISearchBarDelegate {

let whiskeySearchBar = UISearchBar()
var ref: FIRDatabaseReference?
var refHandle: UInt!
var whiskeyList = [WhiskeyItem]()

let cell = "cell"

override func viewDidLoad() {
    
    super.viewDidLoad()
    
    createWhiskeySearchBar()
    
    //Display Firebase whiskey data:
    ref = FIRDatabase.database().reference()
    fetchWhiskey()

}

func createWhiskeySearchBar() {
    
    whiskeySearchBar.showsCancelButton = false
    whiskeySearchBar.placeholder = "Search whiskeys"
    whiskeySearchBar.delegate = self
    
    self.navigationItem.titleView = whiskeySearchBar
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return whiskeyList.count
}


 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
 
 // Configure the cell...
    
 cell.textLabel?.text = whiskeyList[indexPath.row].wName
 
 return cell
 }




func fetchWhiskey() {
    refHandle = ref?.child("whiskey").observe(.childAdded, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String : AnyObject] {
            
            print(dictionary)
            let whiskeyItemInstance = WhiskeyItem()
            
            whiskeyItemInstance.setValuesForKeys(dictionary)
            self.whiskeyList.append(whiskeyItemInstance)
            
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }
    })

}
Questioner
kel
Viewed
0
WarmupBallad 2017-04-27 08:05:13

Your initializer has two parameters which are required when calling it.

Calling it properly would look something like this:

let whiskeyItemInstance = WhiskeyItem(wName: "name", wType: "type")

If you don't want to pass parameters to the initializer, you could provide default params:

init(wName: String = "default name", wType: String = "default type") {

or use an initializer with no parameters at all:

init() {
    self.wName = "wName"
    self.wType = "wType"
}

or call the initializer you already created like so:

convenience init() {
    self.init(wName: "default name", wType: "default type")
}

Or you could forgo initializers altogether:

class WhiskeyItem {
    let wName: String = "asdf"
    let wType: String = "asdf"
}