2016-04-21 28 views
0

我想用他們的用戶名從Firebase列出所有用戶的列表。製作Firebase用戶列表

This is how my users is saved.

我的問題是,我試圖讓所有的用戶的數組。

import UIKit 
import Firebase 

var memberRef = Firebase(url: "\(BASE_URL)/users") 
//var currentUser = DataService.dataService.USER_REF.authData.uid 



var currentUsers: [String] = [String]() 

class dataTableView: UITableViewController 
{ 

    override func viewDidLoad() 
    { 
     loadUsers() 
    } 

    func loadUsers() 
    { 
     // Create a listener for the delta additions to animate new items as they're added 
     memberRef.observeEventType(.ChildAdded, withBlock: { (snap: FDataSnapshot!) in 

      print(currentUsers) 

      // Add the new user to the local array 
      currentUsers.append(snap.value as! String) 

      // Get the index of the current row 
      let row = currentUsers.count - 1 

      // Create an NSIndexPath for the row 
      let indexPath = NSIndexPath(forRow: row, inSection: 0) 

      // Insert the row for the table with an animation 
      self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Top) 

     }) 
    } 

但是,我得到了類型NSDictionary的錯誤傾向轉換爲NSString的值。

+0

請粘貼您的火力地堡結構爲文本,而不是照片。您可以使用Firebase儀表板中的導出按鈕獲取該信息。文本將使我們不必在答案中重新鍵入結構。 – Jay

回答

1

Firebase快照是一個字典,而不是一個字符串,這就是爲什麼你會收到該錯誤。

訪問快照的元素:

if let name = snapshot.value["name"] as? String { 
    print(name) 
} 

你也可以使用

if let name = snapshot.value.objectForKey("name") as? String {