Я пытаюсь присвоить результат запроса Alamofire
переменной класса TableView
.
Я понял, что когда я использую переменную self.notifications в блоке запроса Alamofire
, это работает. Но когда я вызываю self.notifications за пределами Alamofire
, это ноль, и self.notifications не назначается по ссылке. Кажется, что переменная копирования
class NotificationsTableView: UITableViewController{
var notifications: Notification!
let uri = Helper.URLDEV + "/api_x/notifications/"
override func viewDidLoad() {
super.viewDidLoad()
tableView.cellLayoutMarginsFollowReadableWidth = true
self.tableView.delegate = self
self.tableView.dataSource = self
print("Table loaded")
let auth_code = ["Authorization": "Token " + Helper.getMyToken() ]
Alamofire.request(self.uri, parameters: nil, encoding: URLEncoding.default, headers: auth_code).responseJSON { response in
guard let data = response.data else { return }
do {
let decoder = JSONDecoder()
//This is working properly
self.notifications = try decoder.decode(Notification.self, from: data)
print(self.notifications?.results?[2].notificationData?.body ?? 999)
} catch let error {
print(error)
}
}
print("URI: \(uri)")
//Here is just nil, if I didn't assign a value before
print(self.notifications == nil)
Я ожидаю, что self.notification не будет nil после Alamofire
request