前提・実現したいこと
Googleスプレッドシートで作成したJSONデータをTableViewに反映させたいのですが、うまく反映されません。。
スプレッドシートで作成したJSONデータ
発生している問題・エラーメッセージ
シミュレーターでTableViewを開くと下記のメッセージがデバッグエリア?に出てきます。
2019-09-29 21:05:37.142869+0900 cooking[1931:107676] Task <C62F887C-5111-4F23-941E-4348CB485C3D>.<1> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, NSErrorFailingURLKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>, NSUnderlyingError=0x600000b0ac10 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}} failure(Alamofire.AFError.sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, NSErrorFailingURLKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>, NSUnderlyingError=0x600000b0ac10 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}})) sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, NSErrorFailingURLKey=https%3A//script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>" ), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <C62F887C-5111-4F23-941E-4348CB485C3D>.<1>, NSUnderlyingError=0x600000b0ac10 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}})
該当のソースコード
Swift
1import Alamofire 2import UIKit 3import SwiftyJSON 4 5class cookingViewController: UIViewController, UINavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource { 6 7 var googleData = GoogleData() 8 var nameArray = [String]() 9 var amountArray = [String]() 10 var taniArray = [String]() 11 var priceArray = [String]() 12 13 @IBOutlet var tableView: UITableView! 14 15 override func viewDidLoad() { 16 super.viewDidLoad() 17 18 tableView.delegate = self 19 tableView.dataSource = self 20 tableView.allowsMultipleSelection = true // 複数選択可にする 21 22 getData() 23 24 } 25 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 26 return nameArray.count 27 } 28 29 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 30 let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "Cell") 31 cell.selectionStyle = .none //ハイライトさせない 32 cell.textLabel?.text = self.nameArray[indexPath.row] 33 cell.detailTextLabel?.text = self.amountArray[indexPath.row] 34 cell.textLabel?.adjustsFontSizeToFitWidth = true 35 cell.detailTextLabel?.adjustsFontSizeToFitWidth = true 36 cell.textLabel?.numberOfLines = 1 37 cell.detailTextLabel?.numberOfLines = 1 38 cell.accessoryType = .checkmark 39 40 return cell 41 42 } 43 44 func getData(){ 45 let text = "https://script.google.com/macros/s/AKfycbw1IzuulWfaxTtgsNS9Yi5iUNeBqBHy1XC-wV0IOBiAjpzsw98/exec" //取得したいURL 46 let url = text.addingPercentEncoding(withAllowedCharacters: .afURLQueryAllowed) 47 AF.request(url!, method: .get, parameters: nil, encoding: JSONEncoding.default).responseJSON { (response) in 48 //やってること→①requestを送る ②JSON解析 ③値を全てfor文で配列に入れる 49 print(response) 50 switch response.result{ 51 52 case .success: 53 54 for i in 0...10{ 55 56 let json:JSON = JSON(response.data as Any) 57 let name = json["name"][i].string 58 let amount = json["amount"][i].string 59 let tani = json["tani"][i].string 60 let price = json["price"][i].string 61 62 self.nameArray.append(name!) 63 self.amountArray.append(amount!) 64 self.taniArray.append(tani!) 65 self.priceArray.append(price!) 66 67 } 68 break 69 case .failure(let error): 70 print(error) 71 break 72 73 } 74 self.tableView.reloadData() 75 } 76 77 } 78 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 79 80 }
お忙しいところ恐れ入りますが、何卒よろしくお願い致します…
ツールのバージョン
Xcode : Version 11.0
Swift : Apple Swift version 5.1
回答1件
あなたの回答
tips
プレビュー