NoobProgrammer
NoobProgrammer

Reputation: 114

Can't append object to array of objects in swift

So I'm trying to access data from an API that gives me a JSON file with local businesses, I'm using swiftyJSON to get the data from the JSON file and then checking if they are not nil to the assign them to public variables and then create objects(Venues) from said data. But the problem is that I can't append the objects(Venues) to my Venues Array.

Here is my code:

//File Name: ViewController.swift

import UIKit
import QuadratTouch
import SwiftyJSON
import CoreLocation

class ViewController: UITableViewController{

   //Here I create an empty array of Venue Objects  
   var venuesArr = [Venue]()


    override func viewWillAppear(animated: Bool) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        callAPI()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.venuesArr.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell

        cell.textLabel?.text = self.venuesArr[indexPath.row].name
        return cell
    }

    func callAPI(){

        let session = Session.sharedSession()
        var parameters = [Parameter.section: "food" ]
        parameters += [Parameter.near:"Massachusetts"]
        parameters += [Parameter.radius:"16093"]
        parameters += [Parameter.limit:"20"]
        let searchTask = session.venues.explore(parameters) {
            (result) -> Void in
            if let response = result.response {
                myJSON = JSON(response)

                let group = myJSON["groups"][0]

                for var i = 0; i < 20;i++ {

                    var items = group["items"][i]
                    var venue = items["venue"]


                    if venue["name"] != nil{
                        venueName = venue["name"].string as String!
                    }

                    if venue["hours"]["status"] != nil{
                        venueHoursStatus = venue["hours"]["status"].string as String!
                    }

                    if venue["hours"]["isOpen"] != nil{
                        venueIsOpen = Bool(venue["hours"]["isOpen"])
                    }

                    let venueRating = venue["rating"].doubleValue

                    if venue["location"]["address"] != nil{
                        venueStreetAddress = venue["location"]["address"].string as String!
                    }

                    if venue["location"]["city"] != nil{
                        venueCity = venue["location"]["city"].string as String!
                    }

                    if  venue["location"]["postalCode"] != nil{
                        venuePostalCode = venue["location"]["postalCode"].string as String!
                    }

                    if venue["location"]["state"] != nil{
                        venueState = venue["location"]["state"].string as String!
                    }

                    if (venue["hasMenu"] != nil){
                        venueHasMenu = Bool(venue["hasMenu"])
                    }

                    if (venue["menu"]["mobileUrl"] != nil){
                        venueMenuURL = venue["menu"]["mobileUrl"].string as String!
                    }


                    let venueObject = Venue(name: venueName, hoursStatus: venueHoursStatus, isOpen: venueIsOpen, streetAddress: venueStreetAddress, city: venueCity, state: venueState, postalCode: venuePostalCode, hasMenu: venueHasMenu, menuURL: venueMenuURL, rating: venueRating)

                    //Here is the PROBLEM ----------
                    self.venuesArr.append(venueObject) //--------> This gets called and stores the objects
                     //Here is the PROBLEM ----------

                }//end of loop

            }
        }
        searchTask.start()


    }




    // Override point for customization after application launch.


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Here is the class definition for Venue

//File Name: Venue.swift
import Foundation

public class Venue{

var name: String
var hoursStatus: String
var hasMenu: Bool
var isOpen: Bool
var streetAddress : String
var city : String
var postalCode : String
var state: String
var menuURL:String
var rating: Double


init(name:String, hoursStatus: String, isOpen: Bool, streetAddress : String, city : String, state: String, postalCode : String, hasMenu: Bool, menuURL:String, rating: Double){

    self.name = name
    self.hoursStatus = hoursStatus
    self.hasMenu = hasMenu
    self.isOpen = isOpen
    self.streetAddress = streetAddress
    self.city = city
    self.postalCode = postalCode
    self.state = state
    self.menuURL = menuURL
    self.rating = rating

  }   
}

The debugger shows me that the objects are stored, but then when I try to access them, they aren't there anymore. Please help! image of debugger enter image description here

Here is my last file that holds some variables:

//File Name: Variables.swift
import Foundation
import SwiftyJSON

//JSON that contains API info
public var myJSON:JSON!

//Venue variables
public var venueName: String!
public var venueHoursStatus: String!
public var venueIsOpen: Bool!
public var venueStreetAddress: String!
public var venueCity: String!
public var venuePostalCode: String!
public var venueState:String!
public var venueHasMenu: Bool!
public var venueMenuURL: String!

Upvotes: 0

Views: 437

Answers (1)

NoobProgrammer
NoobProgrammer

Reputation: 114

Ultimately I was trying to display the data in the array to a tableView, and then I realized I didn't reload the tableview at the end of callAPI() so I simply inserted self.tableView.reloadData() and then it worked!

I thought the objects weren't being appended into the array, but they were. The actual problem was that I wasn't reloading the datasource of my tableview.

Upvotes: 1

Related Questions