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
}
I'm using swift3 and Firebase to create a messaging app. I need to access the user2 (user who is receiving a message) id in order to add them to the chat based on the way I structured the data. In this function, it returns "NA" for userid
every time, but it also prints "almost there" so I know that info == userName
is true
. It won't reassign (userid = id as! String)
at all though. Any ideas?