func getUser2Info(userName: String) -> String {
var userid:String = "NA"
ref.child("users").observeSingleEvent(of: .value, with: { (snapshot) in
if let users = snapshot.value as! NSDictionary? {
for (id, info) in users {
let userId = id as! String
let userInfo = info as! NSDictionary
for (property, info) in userInfo {
if (property as! String == "username") {
//print(info)
//print(userName)
if (info as! String == userName) {
print("almost there")
userid = id as! String
} else {
userid = "NA"
}
}
}
}
}
})
return userid
}
我正在使用swift3和Firebase来创建一个消息传递应用。我需要访问user2(正在接收消息的用户)id,以便根据我构建数据的方式将它们添加到聊天中。在此函数中,每次都会为userid
返回“NA”,但它也会打印“几乎在那里”,所以我知道info == userName
是true
。但它根本不会重新分配(userid = id as! String)
。有什么想法吗?