引用テキスト現在Swift4でiOSアプリを作成しています。
APiを叩いてJsonを取得しパースした後のデータをテーブルに表示したいのですが
パースした後のデータを代入するための変数var stores : [categories.stores]
を用意したのですが代入ができず値がnilになります。
調べてみたのですが、書き方自体は間違っていないように思います。
どうかご教授いただけると幸いです。
HomeViewController
1class HomeViewController: UIViewController,UITableViewDelegate, UITableViewDataSource { 2 3 @IBOutlet weak var TableView: UITableView! 4 5 6 var category_id = "" 7 let cellReuseIdentifier = "cell" 8 //Json用変数 9 var stores : [categories.stores]? 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 TableView.delegate = self 14 TableView.dataSource = self 15 16 let url = URL(string: "API") 17 let request = URLRequest(url: url!) 18 let session = URLSession.shared 19 20 let encoder: JSONEncoder = JSONEncoder() 21 encoder.dateEncodingStrategy = .iso8601 22 encoder.outputFormatting = .prettyPrinted 23 24 session.dataTask(with: request){(data, response, error)in if error == nil, 25 let data = data, 26 let response = response as? HTTPURLResponse{ 27 28 let decoder: JSONDecoder = JSONDecoder() 29 decoder.dateDecodingStrategy = .iso8601 30 do { 31 32 let json = try decoder.decode(categories.self, from: data) 33 34 //Jsonを変数に代入 35 self.stores = json.stores 36 37 } catch { 38 print("error:", error.localizedDescription) 39 40 } 41 42 } 43 44 }.resume() 45 } 46 47 override func viewDidAppear(_ animated: Bool) { 48 super.viewDidAppear(animated) 49 50 51 } 52 53 override func didReceiveMemoryWarning() { 54 super.didReceiveMemoryWarning() 55 56 } 57 58 59 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 60 return (stores!.count) 61 } 62 63 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 64 guard let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as? TableViewCell 65 else { 66 fatalError("The dequeued cell is not an instance of TableViewCell.") 67 } 68 69 //cell.imageCell.image = UIImage(named: "") 70 71 if let name:String = stores![indexPath.row].name{ 72 cell.storeName.text! = name 73 }else{ 74 cell.storeName!.text = "nil" 75 } 76 if let location : String = stores![indexPath.row].location{ 77 cell.storeLocation.text! = location 78 }else{ 79 cell.storeLocation.text! = "nil" 80 } 81 82 return cell 83 } 84 85 86 87 88} 89
JsonModel
1struct categories : Codable { 2 let id : Int 3 let name : String 4 let stores : [stores] 5 6 struct stores : Codable { 7 let id : Int 8 let name : String 9 let location : String 10 let price : String 11 let open_time : String 12 let closed_day : String 13 let photos : [photos] 14 let tags : [tags] 15 16 struct photos:Codable { 17 let id : Int 18 let path : String 19 } 20 21 struct tags:Codable { 22 let id : Int 23 let name : String 24 } 25 } 26} 27
OutPutJson
1categories 2( 3id: 1, 4name: "誕生日", 5 stores: [Sweemy.categories.stores 6( 7id: 1, 8name: "Pink Cafe Tokyo", 9 location: "東京", 10 price: "1000~3000", 11 open_time: "10:00-14:00", 12 closed_day: "月曜日", 13 photos: [Sweemy.categories.stores.photos 14( 15id: 1, 16path: "photos/Pink Cafe Tokyo_0.jpeg" 17) 18], 19tags: [Sweemy.categories.stores.tags 20( 21id: 1, name: "スイーツ" 22), 23Sweemy.categories.stores.tags 24( 25id: 13, 26name: "ピンクカフェ" 27), 28 Sweemy.categories.stores.tags( 29id: 14, 30name: "コンセプトカフェ" 31) 32] 33)
回答1件
あなたの回答
tips
プレビュー