Skip to content

Instantly share code, notes, and snippets.

@KrauserHuang
Created May 10, 2021 01:28
Show Gist options
  • Save KrauserHuang/580116cc9c12b9f951cd0012e5faa54e to your computer and use it in GitHub Desktop.
Save KrauserHuang/580116cc9c12b9f951cd0012e5faa54e to your computer and use it in GitHub Desktop.
// 抓取訂單資料
func fetchData() {
let urlStr = "https://sheetdb.io/api/v1/ocn6qrzzdg1mn"
let url = URL(string: urlStr)
var request = URLRequest(url: url!)
request.httpMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data {
let decoder = JSONDecoder()
do {
let targetResponse = try decoder.decode([[String:String]].self, from: data)
self.targetList = targetResponse
DispatchQueue.main.async {
self.tableView.reloadData()
self.totalCountLabel.text = "共計\(self.targetList.count)杯"
self.totalPriceLabel.text = "$\(self.computeTotal())"
}
} catch {
print(error)
}
}
}.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment