I have the struct instance called users
and Im parsing the JSON into this instance via closure but I came across that my edits don't save at all. So when I am out of the closure scope I have nil. Somebody know how to capture the value in closure?
var users: [User]! = [User]()
override func viewDidLoad() {
super.viewDidLoad()
Alamofire.request(url2).responseJSON { response in
switch response.result {
case .success(let value):
guard let jsonArray = value as? [[String: Any]] else { return }
self.users = jsonArray.compactMap(User.init)
print(self.users!) // works
case .failure(let error):
print(error)
}
}
print(users!) // error because I had nil
}