我正在尝试使用用户名列出Firebase中的所有用户。
我的问题是我正在尝试制作一个包含所有用户的数组。
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类型的错误cant cast值到NSString。
答案 0 :(得分:1)
Firebase快照是字典,而不是字符串,这就是您收到该错误的原因。
要访问快照的元素:
if let name = snapshot.value["name"] as? String {
print(name)
}
您也可以使用
if let name = snapshot.value.objectForKey("name") as? String {