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

ios-呼叫中缺少参数的参数

(ios - Missing argument for parameter in call)

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

swift(3)和Xcode(8)的新手,并且我正在使用firebase在a中加载一些数据tableView 当我尝试构建应用程序时,出现错误:"Missing argument for parameter name in call"在调用WhiskeyItem实例的行上的fetchWhiskey函数中。我不知道为什么会发生此错误。谁能帮我吗?

这是我的课:

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

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

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

初始化程序有两个调用它们时必需的参数。

正确调用它看起来像这样:

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

如果你不想将参数传递给初始化程序,则可以提供默认参数:

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

或使用完全不带参数的初始化程序:

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

或像这样调用你已经创建的初始化程序:

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

或者,你可以完全放弃初始化程序:

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