Arrays Swift数组将不会填充JSON数据
我面临的问题是,我无法用JSON数据填充空数组,也找不到StackOverflow上的类似问题 在函数本身中,我可以得到空数组并在函数中填充数组。然后在downloadRestaurantDetails函数中调用print函数,查看我解析的信息 但我无法填充函数外部的原始空数组,以便获得填充的数组并在其他函数中使用它Arrays Swift数组将不会填充JSON数据,arrays,json,swift,google-places-api,google-maps-sdk-ios,Arrays,Json,Swift,Google Places Api,Google Maps Sdk Ios,我面临的问题是,我无法用JSON数据填充空数组,也找不到StackOverflow上的类似问题 在函数本身中,我可以得到空数组并在函数中填充数组。然后在downloadRestaurantDetails函数中调用print函数,查看我解析的信息 但我无法填充函数外部的原始空数组,以便获得填充的数组并在其他函数中使用它 import UIKit import GoogleMaps import Alamofire import SwiftyJSON class ViewController: U
import UIKit
import GoogleMaps
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
var placeIDArray = [String]()
var placeID: String!
override func viewDidLoad() {
super.viewDidLoad()
downloadRestaurantDetails { () -> () in
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func downloadRestaurantDetails(completed: DownloadComplete) {
//url is acquired through a file created for global variables
Alamofire.request(.GET,url).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
//Acquire All place_id of restaurants
if let results = json["results"].array {
for result in results {
if let allPlace_ID = result["place_id"].string {
//Add All place_id's into an array
self.placeIDArray.append(allPlace_ID)
}
}
}
}
// i call this method to check and see if there is anything placed in the array outside of the downloadRestaurantDetails method.
func check() {
if self.placeIDArray.count > 1 {
print(self.placeIDArray)
} else {
print(self.placeIDArray.count)
}
}
总之,我想解决的问题
- 我想解析place_id并将结果字符串放入
地点排列。我能够解析数据并将值存储在函数的placeIDArray中。我无法将解析后的数据存储在函数外的空数组中。我曾认为,通过使用self.placeIDArray,解析后的数据将被分配给数组。但事实似乎并非如此李>
下载餐厅详细信息
是异步的。因此,如果您在调用上述函数后立即调用check
,则可能(而且很可能)JSON尚未提取,因此placeIDArray
尚未填充。您必须在回调中调用它,因为此时数据实际上已下载并填充到数组中
因此:
func downloadRestaurantDetails(completed: DownloadComplete) {
//url is acquired through a file created for global variables
Alamofire.request(.GET,url).responseJSON { (response) -> Void in
if let value = response.result.value {
let json = JSON(value)
//Acquire All place_id of restaurants
if let results = json["results"].array {
for result in results {
if let allPlace_ID = result["place_id"].string {
//Add All place_id's into an array
self.placeIDArray.append(allPlace_ID)
}
}
// !!! Call the callback:
completed()
}
}
检查:
override func viewDidLoad() {
super.viewDidLoad()
downloadRestaurantDetails { () -> () in
// The array is now filled.
self.check()
}
}
在这个问题上,我仍然感到困惑,有人知道吗?当您检查数据是否分配给
placeIDArray
时,您试图在哪里访问placeIDArray
的值?我创建了一个函数来检查是否可以访问数组中的数据并将其打印到调试屏幕上,然后在函数中打印时,它会给我一个计数0self.placeIDArray.append(allPlace_ID)然后所有元素都按照我的要求正确打印。因此,我认为这些元素并不是在函数之外赋值的。如果这是正确的逻辑?应在功能外分配元素。您正在回调中调用check()
?例如,downloadRestaurantDetails{()->在self.check()}
?我在回调外部调用了check,但在viewDidLoad.genius内部调用了check!非常感谢!:)解决问题很有趣!我在downloadRestaurantDetails函数中将completed()调用移到倒数第二个closed curly之前,这样它就不会循环无数次。但除此之外,它完美地解决了我的问题!