我正在尝试从Firebase检索数据并将该数据存储在检索该数据的闭包之外.
var stringNames = [String] ()
ref?.observeEventType(.Value, withBlock: { snapshot in
var newNames: [String] = []
for item in snapshot.children {
if let item = item as? FIRDataSnapshot {
let postDict = item.value as! [String: String]
newNames.append(postDict["name"]!)
}
}
stringNames = newNames
})
print(stringNames)
Run Code Online (Sandbox Code Playgroud)
stringNames返回空,但是当我从闭包内打印时,它有正确的数据.非常感谢任何帮助,谢谢!