这是我的问题:我有一个迅速地从json响应获取数据并将其值设置为对象数组的函数(我知道Codable对象,但不起作用)。 在我的函数中,我的对象数组充满了值,我可以检索它们,但是当我调用它并将其结果放入数组中时,它是空的...
这是我的功能:
func getMembers (username:String, password:String) -> [Member] {
var allMembers:[Member] = [Member]()
let parameters: Parameters=[
"action":"getPosition",
"username":"\(username)",
"password":"\(password)"
]
Alamofire.request(login_url, method: .post, parameters: parameters).responseJSON{
response in
switch response.result {
case .success(let data):
guard let json = data as? [String : AnyObject] else { return }
let members = json["family"]!["members"] as! [[String:Any]]
members.forEach { member in
let thisMember = Member(
userID: member["userId"] as! Int,
lasName: member["lasName"] as! String,
firstName: member["firstName"] as! String,
latitude: member["latitude"] as! Double,
longitude: member["longitude"] as! Double)
allMembers.append(thisMember)
}
case .failure(let error):
print(error)
}
}
return allMembers
}
如果我在allMembers上每个都做一个,则我有我的数据。但是正如我所说,当我从“外部”调用它时,该数组为空。像这样:
var members:[Member] = [Member]()
members = getMembers(username: username, password: password)
print(members.count)
所以我的打印返回0 ...(应该返回2)
谢谢您的帮助!