So I always thought that code would execute from top to bottom. For example:
var1 = "one"
var2 = "two"
var3 = "three"
This all works, BUT i have another variable, 'data' that needs to be filled. Here is the function for that.
@objc func getData()
{
print("inside")
let url: URL = URL(string: self.urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
let task = defaultSession.dataTask(with: url) { (data, response, error) in
if error != nil {
print("Error")
}else {
print("downloaded")
self.downloadResult = String(data: data!, encoding: String.Encoding.utf8) as String!
self.data = self.downloadResult
.components(separatedBy: "\n")
.map { $0.components(separatedBy: "\t") }
self.header = self.data[0]
self.data = Array(self.data.dropFirst())
self.data = Array(self.data.dropLast())
}
}
task.resume()
//spreadsheetView.reloadData()
}
So if I want this done, I just gotta add it after my other variables right? Like this:
var1 = "one"
var2 = "two"
var3 = "three"
getData()
But no, it does not seem to do this. Specifically, the 'task' in getData() won't execute - the print "inside" statement will print, though. Can someone explain why the urlsession task wont execute?
After everything is initialized, only then will the full getData function work in its entirety. But I need my data variable filled before. I don't get it. Any insight would be greatly appreciated. I am using Swift 4 on Xcode9.2. Thank you!