i need some clarification regarding passing values out of closures. I think the issues is to use dispatchQueue.main but I cannot understand where and why
here is the code:
import UIKit
class ViewController: UIViewController {
var allCard = [Card]()
let card = Card(name: "", cost: 0, attack: 0, durability: 0, cardClass: "", cardSet: "", imageURL: "", goldenImageURL: "", type: "", mechanics: [["":""]], howToGetGolden: "")
override func viewDidLoad() {
super.viewDidLoad()
getCards()
print(allCard.count)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func getCards() {
let url = URL(string: "https://omgvamp-hearthstone-v1.p.mashape.com/cards/sets/Classic?mashape-key=....")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil { // if urlSession have issues manage the error here
print(error)
} else { // if we have data, response, error then continue here
if let urlContent = data { // if data exist then
do {
let jsonResult = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as! [Dictionary<String, AnyObject>]
for cards in 0...jsonResult.count - 1 {
if let name = jsonResult[cards]["name"] as? String { //NAME Optionaml Bindings
self.card.name = name
} else {
self.card.name = ""
} // END Name Optionaml Bindings
if let cost = jsonResult[cards]["cost"] as? Int { //COST Optionaml Bindings
self.card.cost = cost
} else {
self.card.cost = 0
} // END COST Optionaml Bindings
if let attack = jsonResult[cards]["attack"] as? Int { //ATTACK Optionaml Bindings
self.card.attack = attack
} else {
self.card.attack = 0
} // END TTACK Optionaml Bindings
} catch { //catch error while parsing json
print("error")
}
}
} // end if/ELSE
} // end of task (closure)
task.resume()
}
}
How should I use dispatchQueue.main here? And is it async or synch? I tried to check on internet but I couldn't find an answer I understood /:
Thanks for any help