Reputation: 13
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)
Upvotes: 0
Views: 938
Reputation: 2818
let apiObject = API()
// Your problem it here this method takes time to update informationArray
apiObject.getEarthquakeInformation()
// But this line print immediately while it's still empty.
print(apiObject.informationArray)
what you do is
informationArray
from the API
class at all and put it in MapViewController
change getEarthquakeInformation
method to have a compeletion handler
class API: NSObject {
// remove it //var informationArray = NSMutableArray();
func getEarthquakeInformation(compeletion: (informationArray: [AnyObject]!) ->()) {
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"] {
compeletion(result)
} else {
print("error")
}
}
}
task.resume()
}
and then in MapViewController
var informationArray: [AnyObject]!
override func viewDidLoad() {
super.viewDidLoad()
// Instance of the api class
let apiObject = API()
apiObject.getEarthquakeInformation() { [unowned self] (result) ->() in
self.inforamtionArray = result
print(self.informationArray)
}
Upvotes: 0
Reputation: 2567
Several things here:
First, please look into using a networking library (such as AlamoFire)
Second, when you declare your mutable array, you should do it in this form (No reason to use NSMutableArray()):
var myArray = [ObjectType]()
Third, don't use C-style for-loops, they're flagged to be removed from Swift. You should iterate instead:
for item in result {
//Do something with item.
}
Also, as far as "sending" the array to your MapViewController object. If the API object lives in your MapViewController, then you could have your API function take in a closure as an argument. You can pass the array back to the MapViewController in the closure itself. Or you could also use notifications.
Hope this helps.
Upvotes: 1