gpt4 book ai didi

arrays - 无法快速将对象追加到对象数组中

转载 作者:行者123 更新时间:2023-11-30 10:08:40 25 4
gpt4 key购买 nike

因此,我尝试从 API 访问数据,该 API 为我提供了本地企业的 JSON 文件,我正在使用 swiftyJSON从 JSON 文件获取数据,然后检查它们是否不为零,将它们分配给公共(public)变量,然后从所述数据创建对象( field )。但问题是我无法将对象( field )附加到我的 field 数组中。

这是我的代码:

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

最佳答案

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

我以为对象没有被附加到数组中,但它们确实被附加到了数组中。实际的问题是我没有重新加载表格 View 的数据源。

关于arrays - 无法快速将对象追加到对象数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34502788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com