无法将对象附加到 swift 中的对象数组
Can't append object to array of objects in swift
因此,我正在尝试从 API
访问数据,该 JSON
文件包含本地企业,我正在使用 swiftyJSON
从 JSON
文件,然后检查它们是否不为零,将它们分配给 public 变量,然后从所述数据创建对象(场地)。但问题是我无法将对象(Venues)附加到我的 Venues 数组。
这是我的代码:
//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.
}
}
这是场地class的定义
//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
}
}
调试器显示对象已存储,但当我尝试访问它们时,它们不再存在。请帮忙!
调试器图像
这是我最后一个包含一些变量的文件:
//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!
最终我试图将数组中的数据显示到 tableView,然后我意识到我没有在 callAPI()
末尾重新加载 tableview 所以我只是插入 self.tableView.reloadData()
和然后它起作用了!
我以为对象没有被追加到数组中,但它们被追加了。实际问题是我没有重新加载 tableview 的数据源。
因此,我正在尝试从 API
访问数据,该 JSON
文件包含本地企业,我正在使用 swiftyJSON
从 JSON
文件,然后检查它们是否不为零,将它们分配给 public 变量,然后从所述数据创建对象(场地)。但问题是我无法将对象(Venues)附加到我的 Venues 数组。
这是我的代码:
//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.
}
}
这是场地class的定义
//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
}
}
调试器显示对象已存储,但当我尝试访问它们时,它们不再存在。请帮忙!
调试器图像
这是我最后一个包含一些变量的文件:
//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!
最终我试图将数组中的数据显示到 tableView,然后我意识到我没有在 callAPI()
末尾重新加载 tableview 所以我只是插入 self.tableView.reloadData()
和然后它起作用了!
我以为对象没有被追加到数组中,但它们被追加了。实际问题是我没有重新加载 tableview 的数据源。