I'm a bit of a Swift newbie, and I've run into a problem that is really stumping me...
In the code below:
class TestViewController: UIViewController {
var userNames : [String] = {return DatabaseMethods.getUsers()}()
override func viewDidLoad() {
super.viewDidLoad()
print(userNames)
}
}
The print statement will print an empty array every time. (I) I'm not sure why this is happening, and (II) I haven't figured out a way to get around this issue. Just for a bit more context, Here is what the DatabaseMethods class looks like:
class DatabaseMethods {
static func getUsers() -> [String] {
var userNames = [String]()
db.collection("users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let data = document.data()
let first : String = data["First"] as! String
let last : String = data["Last"] as! String
userNames.append("\(first) \(last)")
}
}
}
return userNames
}
}