I am developing an app with Xcode and the Swift programming language. Keep in mind my app works closely with Firebase. I created a function to retrieve a parameter from Firebase based on a key.
// My global variables. 'ref' is my firebase database and 'returnValue' is the value I am trying to write to withing the closure.
var ref = Database.database().reference(withPath: "accounts")
var returnValue: String = ""
func getParam(key: String) -> String {
let index = defs.integer(forKey: "index")
self.returnValue = "Failed retrieving parameter in getParam() function"
self.ref.child("data").observeSingleEvent(of: .value) { (snapshot) in
let data = snapshot.value! as! NSArray
let dictionary = data[index] as! NSDictionary
self.returnValue = String(describing: dictionary[key])
print("returnValue:", self.returnValue) //First line in console
return
}
return self.returnValue
}
override func viewDidLoad() {
super.viewDidLoad()
print(getParam("status")) //Second line in console
}
Whenever I call this function I see this in the console:
returnValue: Optional(0) //take in mind this is the output I am looking for in the second line
Failed retrieving parameter in getParam() function