Análise JSON rápida, matriz não tem valor fora de NSURLSession
Estou tentando chamar um json webservice em rápido, com o seguinte código e exibi-lo emtableview
no IOS rápido.
/*declared as global*/ var IdDEc = [String]() // string array declared globally
//inside viewdidload
let url = NSURL(string: "http://192.1.2.3/PhpProject1/getFullJson.php")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
let json1 = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("json string is = ",json1) // i am getting response here
let data = json1!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as! NSArray
for arrayData in json {
let notId = arrayData["ID"] as! String
self.IdDEc.append(notId)
}
print(" id = ",IdDEc) //here i am getting values
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
print(" id out count = ",self.IdDEc.count) //here also
}
print(" id out count = ",self.IdDEc.count) // not here
task.resume()
declarei a matriz IdDEc como global, ainda assim, o escopo dessa matriz reside apenas dentro do NSURLSession,
Também eu quero usar essa matriz para preencher tableview. Aqui está o arquivo de saída json de amostra
[
{"ID":"123" , "USER":"philip","AGE":"23"},
{"ID":"344","USER":"taylor","AGE":"29"},
{"ID":"5464","USER":"baker","AGE":"45"},
{"ID":"456","USER":"Catherine","AGE":"34"}
]
Eu sou um novato em rápido por favor me ajude