0

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)
5
  • 1
    Your call (NSURLSession thing) is async. You'd print "print(apiObject.informationArray)" before "print(self.informationArray[i])". Use a block (closure I think in Swift) Commented Jan 20, 2016 at 15:27
  • 3
    What's the problem? What's the wall that you hit? Commented Jan 20, 2016 at 15:27
  • What is the error or print output? Commented Jan 20, 2016 at 15:31
  • i dont 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. Commented Jan 20, 2016 at 15:47
  • Please clarify your question or run the risk of deletion. Commented Jan 20, 2016 at 16:58

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
  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

  • remove 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)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.