実装したい機能
RealtimeDatabase(Firebase) に保存しているデータを、TableView の各 Label に表示させたいです。出力イメージを下記に添付しています。
データベースは下記のように保存しています。
データベースからシリアルナンバー(number)でフィルターをかけて、(time)(status)(price)を履歴として表示したいと思っています。
コード記述でエラーは返ってこないのですが、この画面に遷移するとアプリが強制終了します。
下にログに記述されていたエラー文を記載します。
発生している問題・エラーメッセージ
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<nasucard.HomeViewControllerTableViewCell 0x7fbda4963400> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key price.'
日本語に訳すと、
キャッチされない例外'NSUnknownKeyException'によるアプリの終了 *理由* [<nasucard.HomeViewControllerTableViewCell 0x7ff629028e00> setValue:forUndefinedKey:] このクラスは、キー"price"の"key value coding"に準拠していません
と書いてありました。
現在記述してるコードのどこかがおかしいんだと思うんですが全然検討がつかずお手上げ状態です・・・
詳しい方がいらっしゃいましたらご教授いただければと思い投稿させて頂きました。
該当のソースコード
HomeViewController
1import UIKit 2 3import Firebase 4import SwiftyJSON 5import FirebaseAuth 6import FirebaseDatabase 7 8class History { 9 10 var time:String? 11 var status:String? 12 var price:Int? 13 14 init(time: String?, status: String?, price: Int?) { 15 self.time = time 16 self.status = status 17 self.price = price 18 } 19 20} 21 22class HomeViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 23 24 25 @IBOutlet weak var tableView: UITableView! 26 27 var ref:DatabaseReference! 28 var argString = "" //遷移元TextFieldに入力されたシリアルナンバーを値渡し 29 30 31 // 履歴をテーブルビューに表示する場所を確保 32 var historys = [History]() 33 34 //リストに表示する件数はある分すべてに設定しています 35 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 36 return historys.count 37 } 38 39 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 40 let cell = tableView.dequeueReusableCell(withIdentifier: "historyCell", for: indexPath) as! HomeViewControllerTableViewCell 41 let history = historys[indexPath.row] 42 cell.timeLabel.text = history.time 43 cell.statusLabel.text = history.status 44 cell.priceLabel.text = "(history.price ?? 0)" 45 return cell 46 } 47 48 49 *** 中略 *** 50 51 52 // ▼▼▼ クラス読み込み時に一度だけ実行される処理 ▼▼▼ 53 54 override func viewDidLoad() { 55 super.viewDidLoad() 56 57 tableView.dataSource = self 58 tableView.delegate = self 59 60 61 //残高に表示するデータをデータベースから取り出し //こっちは上手くいっています 62 ref = Database.database().reference().child("Action") 63 64 ref.queryOrdered(byChild: "number").queryEqual(toValue: argString).queryLimited(toLast: 1).observeSingleEvent(of: .value, with: {(snapshot) in 65 for item in snapshot.children { 66 let snap = item as! DataSnapshot 67 let dict = snap.value as! [String: Any] 68 let pricedata = dict["balance"] as! Int 69 // データの中身 70 print(pricedata as Any) 71 self.balance = pricedata 72 self.price.text = String(self.balance) + String(self.entext) 73 } 74 }) 75 76 //データベースから履歴に表示させるデータを取り出す 77 func LoadHistory() { 78 ref.queryOrdered(byChild: "number").queryEqual(toValue: argString).observeSingleEvent(of: .value, with: {(snapshot) in 79 for item2 in snapshot.children { 80 let snap2 = item2 as! DataSnapshot 81 let dict2 = snap2.value as! [String: Any] 82 let time = dict2["time"] as? String 83 let status = dict2["status"] as? String 84 let price = dict2["price"] as? Int // "price"は Int型 85 print(dict2 as Any) //確認用でdict2を出力(※下記参照) 86 let hist = History(time: time, status: status, price: price) 87 self.historys.append(hist) 88 } 89 self.tableView.reloadData() 90 }) 91 } 92 93 LoadHistory() 94 95 } 96 // ▲▲▲ ここまで ▲▲▲ 97 98 99 override func didReceiveMemoryWarning() { 100 super.didReceiveMemoryWarning() 101 } 102 103} 104
HomeViewControllerTableViewCell
1//TableViewCellにセットしているクラス 2 3import UIKit 4 5class HomeViewControllerTableViewCell: UITableViewCell { 6 7 @IBOutlet weak var timeLabel: UILabel! 8 @IBOutlet weak var statusLabel: UILabel! 9 @IBOutlet weak var priceLabel: UILabel! 10 11 12 13 override func awakeFromNib() { 14 super.awakeFromNib() 15 // Initialization code 16 } 17 18 override func setSelected(_ selected: Bool, animated: Bool) { 19 super.setSelected(selected, animated: animated) 20 } 21 22} 23
###質問者のコメント
よろしくおねがいします(涙)
補足情報(FW/ツールのバージョンなど)
Swift5
Xcode
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/24 11:12
2019/08/24 11:24
2019/08/24 11:33