Created
May 10, 2021 01:28
-
-
Save KrauserHuang/580116cc9c12b9f951cd0012e5faa54e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 抓取訂單資料 | |
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