无法在swift中将对象追加到对象数组中

时间:2015-12-29 00:30:47

标签: arrays swift api swifty-json

因此,我尝试访问API的数据,该数据为我提供了一个包含本地商家的JSON文件,我使用swiftyJSON从中获取数据JSON文件,然后检查它们是否为零,将它们分配给公共变量,然后从所述数据创建对象(场地)。但问题是我不能将对象(场地)附加到我的场地阵列。

这是我的代码:

//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.
    }


}

这是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

  }   
}

调试器向我显示存储了对象,但是当我尝试访问它们时,它们就不再存在了。请帮忙! 调试器enter image description here

的图像

这是我的最后一个包含一些变量的文件:

//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!

1 个答案:

答案 0 :(得分:1)

最终我试图将数组中的数据显示到tableView,然后我意识到我没有在callAPI()的末尾重新加载tableview,所以我只是插入了self.tableView.reloadData()然后它成功了!

我认为对象没有被附加到数组中,但它们是。实际问题是我没有重新加载tableview的数据源。