2017-04-26 146 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" 
}