2017-04-26 123 views
0

swift(3)和xcode(8)的新手,我正在使用firebase在tableview中加载一些数据。当我尝试构建应用程序时,出现错误:当我调用WhiskeyItem的一个实例时,在线上的fetchWhiskey函数中出现“参数wName在调用中缺少参数”。我无法弄清楚为什么会出现这个错误。谁能帮我吗?在调用中缺少参数参数

这里是我的类:

import UIKit 
class WhiskeyItem { 
    let wName: String 
    let wType: String 

    init(wName: String, wType: String) { 
     self.wName = wName 
     self.wType = wType 
    } 
} 

和这里的,即时通讯试图加载数据的实现代码如下:

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

} 
+1

那么,你对WhiskeyItem *初始化*需要两件 - 至少由大家展示一下代码。 * wName *和* wType *。然而,再次,你所显示的代码 - 你也不提供。我的问题是为什么你认为这是Swift 3?看起来(对我而言)更基本。此代码**有没有**工作? – dfd

回答

1

您初始化具有调用它时需要两个参数。

调用它正确地将是这个样子:

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

如果你不想传递参数初始化,您可以提供默认PARAMS:

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