I'm making a network call and retrieving some information from an API and storing it in an NSMutableArray. I hit a wall when trying to send that array info over to another object:
I don't have any errors. I am not able to access the array information in another class. It prints fine in the API class but when trying to access in another class the array prints empty.
This is my API class with an NSMutableArray at the very top to hold the info:
class API: NSObject {
var informationArray = NSMutableArray();
func getEarthquakeInformation() {
let session = NSURLSession.sharedSession()
let urlString = "http://ehp2-earthquake.wr.usgs.gov/fdsnws/event/1/query?format=geojson&limit=20"
let url = NSURL(string: urlString)!
let request = NSURLRequest(URL: url)
let task = session.dataTaskWithRequest(request){ data,response,downloadError in
if let error = downloadError {
print("could not complete the request\(error)")
} else {
let parsedResult = try! NSJSONSerialization.JSONObjectWithData(data! , options: NSJSONReadingOptions.AllowFragments)
let dataDict = parsedResult as! NSDictionary
// This holds all the information we need from the API.
if let result = dataDict["features"] {
for var i = 0; i < result.count; i++ {
self.informationArray.addObject(result[i])
print(self.informationArray[i])
}
} else {
print("error")
}
//print("info from the array \(self.informationArray)")
}
}
task.resume()
}
}
And this is the class I'm trying to send it over to: MapViewController
override func viewDidLoad() {
super.viewDidLoad()
// Instance of the api class
let apiObject = API()
apiObject.getEarthquakeInformation()
print(apiObject.informationArray)