简体   繁体   English

调用中缺少参数的参数

[英]Missing argument for parameter in call

New to swift (3) and Xcode (8) and I'm using firebase to load some data in a tableView . swift (3) 和 Xcode (8) 的新手,我正在使用 firebase 在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.当我尝试构建应用程序时,当我调用 WhiskeyItem 的实例时,在我的 fetchWhiskey 函数中出现错误: "Missing argument for parameter name in call" 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:这是我试图在其中加载数据的tableView

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()
            }
        }
    })

}

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"
}

If you want to inheritance class initialize then you can do it like this way如果你想继承类初始化那么你可以这样做

class Room {
    var type: String

    var client: Client

    init(type: String, client: Client) {
        self.type = type

        self.client = client

    }

let unknown = Client() // Need to define it more precisely

    convenience init(type: String) {
        self.type = type

        self.client = unknown

    }

}

Apart from this you can also do these ways除此之外你还可以通过这些方式

The initializer for Room has two parameters (type and client) and neither parameter has a specified default argument, so you must pass an argument of the correct type to each parameter. Room 的初始值设定项有两个参数(类型和客户端),并且两个参数都没有指定的默认参数,因此您必须将正确类型的参数传递给每个参数。 Same story for Client.客户的相同故事。

let theClient = Client(name: "Thelonius", surname: "Monk")
let oneRoom = Room(type: "single", client: theClient)

For more updates: https://developer.apple.com/forums/thread/67555更多更新: https : //developer.apple.com/forums/thread/67555

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM