2016-04-21 78 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 {