at my form load, i need to get a data from json string and push it into an arraylist. When i check my code with breakpoints, my cursor enters my method, going through, and it get task2, pass "if" then stopped at var sec="" then exit my method, after 2-3 seconds, it enters do for. Whats wrong with my code?
override func viewDidLoad() {
super.viewDidLoad()
let urlJsonToken2 = "https://services.domain.com/"
let myURL2 = NSURL(string: urlJsonToken2)
let request2 = NSMutableURLRequest(url: myURL2! as URL)
request2.httpMethod = "GET"
request2.addValue("Bearer "+tokenNewId, forHTTPHeaderField: "Authorization")
request2.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request2.setValue("application/json", forHTTPHeaderField: "Accept")
let task2 = URLSession.shared.dataTask(with: request2 as URLRequest) {(data2, response2, error2) -> Void in
if let unwrappedData2 = data2 {
do {
guard let records = try? JSONSerialization.jsonObject(with: unwrappedData2, options: .mutableContainers) as? [[String: Any]] else {
return
}
for item in records! {
let id = item["id"] as? intmax_t
let name = item["name"] as? String
self?.ArrayList.append(ClassCat(id:id!, id: name!, name));
}
} catch {}
}
} task2.resume()
var sec = ""
}