Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Grabbing array from closure

I have parsed data from the Foursquare API to practice JSON parsing and everything seems to work fine when I did it for a tableView because all I needed to do was reloadData(). Now I am trying to do the same thing but putting it on a map.. I got the location I just can't seem to use the array outside of the closure. Spent quite some time on it but just can't seem to figure it out. Any help would be much appreciated. Thank you.

class ViewController: UIViewController {

  var foursqareArray: [ParseFoursquare]!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

  }

  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    foursqareArray = [ParseFoursquare]()
    downloadFoursquareData { (response) in


      for i in response {
        self.foursqareArray.append(i)
      }
      print("success")
      print(self.foursqareArray)
      // Everything works fine here, I get all 30 venues

    }

    // I get 0 here for some reason that I can't figure out why.
    print(foursqareArray.count)
  }
func downloadFoursquareData(callback: @escaping ([ParseFoursquare]) -> ())  {
    var four2 = [ParseFoursquare]()
    Alamofire.request(Constants.baseURL).responseJSON { response  in
      guard let result = response.result.value as? [String: AnyObject] else {
        return
      }

      guard let response = result["response"] as? [String: AnyObject] else {
        return
      }

      guard let groups = response["groups"] as? [[String: AnyObject]] else {
        return
      }

      for i in groups {
        guard let items = i["items"] as? [[String: AnyObject]] else {
          print("error parsing items")
          return
        }

        for item in items {
          guard let venue = item["venue"] as? [String: AnyObject] else {
            print("error parsing venue")
            return
          }

          guard let location = venue["location"] as? [String: AnyObject] else {
            print("error parsing location")
            return
          }

          guard let latitude = location["lat"] as? Double else {
            print("error parsing latitude")
            return
          }

          guard let longitude = location["lng"] as? Double else {
            print("error parsing longitude")
            return
          }

          guard let name = venue["name"] as? String else {
            print("error parsing name")
            return
          }

          four2.append(ParseFoursquare(longitude: longitude, latitude: latitude, title: name))
        }
        callback(four2)

      }


    }
  }