2017-09-03 46 views
0

我有以下無錯代碼,並且似乎無法使用任何用於我的結果數組的信息。玩過之後,我可以打印列出的每個businessID位置,但我無法添加/使用用於保存此信息的數組。我不明白爲什麼每個位置都打印出來,但我的數組顯示爲空。Google Places API請求返回空數組,但打印所有響應 - 無錯誤

我爲我的代碼全部道歉,但我花了幾個小時試圖糾正我的問題......

import UIKit 
import GooglePlaces 

class FoodTwo: UITableViewController { 

var placesClient: GMSPlacesClient! 

let international: [String] = ["ChIJAQDABX7CIogRIEY3k6r7R-g", 
"ChIJqX3q1IbCIogRFcuI05IPNDU", 
"ChIJAQDABX7CIogRY2MA6XVas8E"] 

let american: [String] = ["ChIJkX9tTSvoIogROXkxd0gpg3s", "ChIJy7lUZCfoIogRVBuB9jWKHUk", "ChIJyzCZMiroIogRkuMavnKsA0w", "ChIJbYvWJ5jCIogRxh0VQA_yD0I", "ChIJa4Sks23CIogRpRod4v5GEN8", "ChIJxVpGsNbpIogRG5HIAKbNyDU", "ChIJ1W32UyvoIogRyp_Rdxn6f8I", "ChIJwTht4ifoIogRsuXdEOrKGMk", "ChIJ6UXEgNPpIogR4Q3ZAAWQQSI", "ChIJUZVAjdTpIogRpyca26a6D8o",     "ChIJ6-h6_EctIIgRO1kypozaGGs", "ChIJK8NGam7CIogRlzU1TKeSjVI", "ChIJ7Xxh1m3CIogRZ_yabslUzd8", "ChIJ_dxSGJ7CIogRcYwJhjAm7TQ"] 

// more arrays here - deleted to reduce scrolling// 

var results = [GMSPlace]() 

var index: IndexPath! 

override func viewDidLoad() { 
    super.viewDidLoad() 

placesClient = GMSPlacesClient.shared() 
    var place: [String] 

    switch index.row 
    { 
    case 0 : 
     place = international 
    case 1 : 
     place = american 
    case 2 : 
     place = asian 
    case 3 : 
     place = bakery 
    case 4 : 
     place = bar 
    case 5 : 
     place = indian 
    case 6 : 
     place = italian 
    default : 
     place = mexican 
    } 
    for id in place 
    { 
     placesClient.lookUpPlaceID(id, callback: { (result, error) -> Void in 

      if let error = error { 
       print("lookup place id query error: \(error.localizedDescription)") 
       return 
      } 

      guard let result = result 
      else 
      { 
      print("No place details for \(id)") 
      return 
      } 
     self.results.append(result) 

     }) 
     OperationQueue.main.addOperation({() -> Void in 
      self.tableView.reloadData() 
       }) 

     } 
} 

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "FCT") as! FoodCellTwo 

    let each = results[indexPath.row] 
    cell.nameLabel.text = each.name 

    return cell 
} 
override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 

} 
} 

回答

1

placesClient.lookUpPlaceID異步函數。你必須等到它完成。所以,你實際上在之前重新加載了表格視圖

您應該在之後重新加載表格視圖數組已填充。

for id in place { 
    placesClient.lookUpPlaceID(id) { result, error in 
     if let error = error { 
      print("lookup place id query error: \(error.localizedDescription)") 
      return 
     } 

     guard let result = result else { 
      print("No place details for \(id)") 
      return 
     } 

     self.results.append(result) 

     if place.count == self.results.count { 
      self.tableView.reloadData() 
     } 
    } 
} 
+0

需要一個更近的支架],但這是答案! – jonpeter

相關問題