2

I was doing the JSON Parsing in Swift tutorial by Ray Wenderlich.

While using DataManager.getTopAppsDataFromAppsWithSuccess in viewDidLoad(), Its unable to print out the array when its outside the DataManager block.

Does someone know how to get the array values outside that block ?

Code :

var apps = [String]()
override func viewDidLoad() {
  DataManager.getTopAppsDataFromAppsWithSuccess { (ByodData) -> Void in
    let json = JSON(data: ByodData)
       if let appArray = json.array{
        for appDict in appArray {
          var appName: String = appDict["name"].stringValue
          self.apps.append(appName)
         }
      }
  println(self.apps)
}

Input JSON:

[
   {
       "id":1,
       "name":"AdobeReader",
       "url":"comp2014group1.herokuapp.com/apps/1.json";
   },
   {
       "id":2,
       "name":"BBCiPlayer",
       "url":"comp2014group1.herokuapp.com/apps/2.json";
   },
   {
       "id":3,
       "name":"BBCNews",
       "url":"comp2014group1.herokuapp.com/apps/3.json";
   },
   {
       "id":4,
       "name":"BBCWeather",
       "url":"comp2014group1.herokuapp.com/apps/4.json";
   }
]

Output :

[]
5
  • Can you also specify your json data you are loading ? Commented Mar 26, 2015 at 3:03
  • The JSON Data is as follows : [{"id":1,"name":"AdobeReader","url":"comp2014group1.herokuapp.com/apps/1.json"},{"id":2,"name":"BBCiPlayer","url":"comp2014group1.herokuapp.com/apps/2.json"},{"id":3,"name":"BBCNews","url":"comp2014group1.herokuapp.com/apps/3.json"},{"id":4,"name":"BBCWeather","url":"comp2014group1.herokuapp.com/apps/4.json"}] Commented Mar 26, 2015 at 7:34
  • You should have added it to the question text for more readability Commented Mar 26, 2015 at 7:51
  • So Is there any way to get around this, basically getting the app names and displaying them in a UIPicker ? Commented Mar 26, 2015 at 9:08
  • @skyline75489 is right you are using the println statement too soon for the data to be loaded. Commented Mar 26, 2015 at 9:11

1 Answer 1

1

This function getTopAppsDataFromAppsWithSuccess seems to be an async one, which will not block execution. Thus the println(self.apps) is immediately executed before the data is actually retrieved.

The JSON data should be OK. Just don't try to print it like that. You can use the JSON data after it's ready.

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

4 Comments

I was wanting to display the array of apps on a UIPickerView but when I use the self.apps I get a blank picker. Is there anyway I can pull the array from the DataManager block
You can use reloadAllComponents function of UIPickerView in the block to refresh the picker view using the data retrieved. However, viewDidLoad may not be the perfect place to do this. Because viewDidLoad is called before the view is loaded. The picker view itself may not be ready when you try to refresh it.
So Is there any way to get around this, basically getting the app names and displaying them in a UIPicker ?
The easiest way to do this will be using sync method, which might cause a little lagging. But the data you are dealing with doesn't seem to be very large. So it should be OK.

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.