JSON parsing swift, array has no value outside NSURLSession
Solution 1:
The idea is to use a "callback".
Here, I've made one for the NSArray you want to get:
completion: (dataArray: NSArray)->()
We create a function to get the array, and we add this callback to the function's signature:
func getDataArray(urlString: String, completion: (dataArray: NSArray)->())
and as soon as the array is ready we'll use the callback:
completion(dataArray: theNSArray)
Here's how the complete function could look like:
func getDataArray(urlString: String, completion: (dataArray: NSArray)->()) {
if let url = NSURL(string: urlString) {
NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
if error == nil {
if let data = data,
json1 = NSString(data: data, encoding: NSUTF8StringEncoding),
data1 = json1.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data1, options: [])
if let jsonArray = json as? NSArray {
completion(dataArray: jsonArray)
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else {
print("Error: no data")
}
} else {
print(error!.localizedDescription)
}
}.resume()
}
}
Now we use this function like this, no more asynchronous issues:
getDataArray("http://192.1.2.3/PhpProject1/getFullJson.php") { (dataArray) in
for dataDictionary in dataArray {
if let notId = dataDictionary["ID"] as? String {
self.IdDEc.append(notId)
}
}
print("id out count = ", self.IdDEc.count)
}
Swift 3 update + fixes and improvements.
func getContent(from url: String, completion: @escaping ([[String: Any]])->()) {
if let url = URL(string: url) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error == nil {
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
if let content = json as? [[String: Any]] { // array of dictionaries
completion(content)
}
} catch {
// error while decoding JSON
print(error.localizedDescription)
}
} else {
print("Error: no data")
}
} else {
// network-related error
print(error!.localizedDescription)
}
}.resume()
}
}
getContent(from: "http://192.1.2.3/PhpProject1/getFullJson.php") { (result) in
// 'result' is what was given to 'completion', an array of dictionaries
for dataDictionary in result {
if let notId = dataDictionary["ID"] as? String {
// ...
}
}
}