質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

2回答

4505閲覧

favTableViewのエラー 「this class is not key value coding-compliant for the key favTableView.」 の解決方法

shuntar30

総合スコア80

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2020/04/20 03:24

編集2020/04/20 04:25

実現したいこと

Swiftで折り紙の折り図を表示するアプリを作っているのですが、UITabBarの「お気に入り」を押すと、「this class is not key value coding-complant for tha key favTableView.」というエラーが出て困っています。回答お願いします。
「お気に入り」ボタン

試したこと

  • Outlet接続を確認して、つなぎ直した
  • SwiftのソースコードでもDelegateとDataSourceをSelfにした
  • 名前が半角英字かを確認した -> 半角英字だった
  • 変数を宣言しているところを消して、もう一度入れ直した
  • StoryBoardでTableViewを消して、もう一度ドラッグした

できることは全部やったつもりですが、ダメでした。

エラーメッセージ

2020-04-20 11:57:08.639195+0900 Origami[5967:370916] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x100e1e9a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key favTableView.' *** First throw call stack: (0x1a4c755f0 0x1a4997bcc 0x1a4b7a9e8 0x1a4f5b0bc 0x1a86d7ad0 0x1a89b8148 0x1a4b50ba0 0x1a89b5300 0x1a86de86c 0x1a86df2e4 0x1a86df59c 0x1a86522dc 0x1a86525fc 0x1a86534a0 0x1a8654790 0x1a8637cf0 0x1db692fb0 0x1a91cec7c 0x1ab7684ac 0x1ab76e604 0x1ab779148 0x1ab6c1e34 0x1ab6eb7c4 0x1a8d18f40 0x1a8dbbba4 0x1a8dbbbfc 0x1a8dbbbfc 0x1a8db38d0 0x1a4bf3b64 0x1a4bf3abc 0x1a4bf3244 0x1a4bee274 0x1a4bedc34 0x1aed3738c 0x1a8d2022c 0x1008b18dc 0x1a4a75800) libc++abi.dylib: terminating with uncaught exception of type NSException

ソースコード

FavoriteViewController.swift

Swift

1// 2// FavoriteViewController.swift 3// Origami 4// 5// Created by ________ ________ on 4/16/20. 6// Copyright © 2020 ________ ________. All rights reserved. 7// 8 9import UIKit 10import os.log 11 12class FavoriteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 13 14 // テーブルビューインスタンス 15 @IBOutlet weak var favTableView: UITableView! // ここがおかしいのか? 16 17 // (func prepare) に (indexPath.row) を渡すための変数 18 var index: String? 19 20 // テーブルビューに表示する配列 21 private var items: Array<String> = [] 22 23 // 検索結果が入る配列 24 private var searchResult: Array<String> = [] 25 26 // Documentsディレクトリのパスを取得 27 let documentPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].absoluteString 28 29 func fav(path: String) { 30 let fav = try? FileManager.default.contentsOfDirectory(atPath: path) 31 32 guard let favorite = fav else { 33 os_log("=> \nNo name to submit") 34 return 35 } 36 37 if favorite.count != 0 { 38 for value in favorite { 39 items.append(value) 40 } 41 } 42 } 43 44 override func viewDidLoad() { 45 super.viewDidLoad() 46 47 print("FavVC") 48 49 fav(path: documentPath) 50 51 favTableView.delegate = self 52 favTableView.dataSource = self 53 54 favTableView.reloadData() 55 56 } 57 58 override func viewDidLayoutSubviews() { 59 super.viewDidLayoutSubviews() 60 } 61 62 // MARK: - TableView Delegate Methods 63 64 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 65 os_log("=> \ntableView") 66 print("tableView() -> Int") 67 print("") 68 return searchResult.count 69 } 70 71 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 72 os_log("=> \ntableView") 73 print("tableView() -> UITableViewCell") 74 print("") 75 // セルを取得する 76 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "FavoriteCell", for: indexPath) 77 78 fav(path: documentPath) 79 80 // セルに表示する値を設定する 81 cell.textLabel!.text = items[indexPath.row] 82 cell.textLabel!.font = UIFont(name: "system", size: 28) 83 84 return cell 85 } 86 87 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 88 self.index = items[Int(indexPath.row)] 89 self.performSegue(withIdentifier: "FavoriteSegue", sender: nil) 90 } 91 92 // MARK: - prepare 93 94 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 95 if segue.identifier == "FavoriteSegue" { 96 if let origamiView: OrigamiView = segue.destination as? OrigamiView { 97 origamiView.image = UIImage(imageLiteralResourceName: self.index! + ".png") 98 origamiView.titleText = self.index! 99 os_log("=> \nSegue to OrigamiVC") 100 print("(self.index!) selected") 101 print("titleText: (self.index!)") 102 print("image: (self.index!).png") 103 print("") 104 } 105 } 106 } 107 108} 109 110

ViewController.swift

Swift

1// 2// ViewController.swift 3// Origami 4// 5// Created by ________ ________ on 4/16/20. 6// Copyright © 2020 ________ ________. All rights reserved. 7// 8 9import UIKit 10import os.log 11 12class ViewController: UIViewController, UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource { 13 14 // MARK: - Variables 15 16 // SearchBarインスタンス 17 @IBOutlet weak var searchBar: UISearchBar! 18 19 // テーブルビューインスタンス 20 @IBOutlet weak var myTableView: UITableView! 21 22 // (func prepare) に (indexPath.row) を渡すための変数 23 var index: String? 24 25 // テーブルビューに表示する配列 26 private var items: Array<String> = [] 27 28 // 検索結果が入る配列 29 private var searchResult: Array<String> = [] 30 31 // MARK: - Functions 32 33 override func viewDidLoad() { 34 super.viewDidLoad() 35 36 print("HomeVC") 37 38 // MARK: - ここに作品名を入力 39 items = ["かみひこうき", "ぱんつ", "つる", "薗部式ユニット ユニット", "薗部式ユニット 組み方"] 40 // MARK: - 作品名終了 41 searchResult = items 42 // MARK: TableView関連 43 // デリゲートの設定 44 searchBar.delegate = self 45 46 myTableView.delegate = self 47 myTableView.dataSource = self 48 } 49 50 override func viewDidLayoutSubviews() { 51 super.viewDidLayoutSubviews() 52 } 53 54 // MARK: - 渡された文字列を含む要素を検索し、テーブルビューを再表示する 55 func searchItems(searchText: String) { 56 os_log("=> \nsearch") 57 print("searchItems(searchText: (searchText))") 58 print("items: (items)") 59 searchResult.removeAll() 60 //要素を検索する 61 if searchText != "" { 62 do { 63 let c = items.count - 1 64 var i = 0 65 var searchWord = items[i] 66 print("loop: (c + 1)") 67 while i <= c { 68 searchWord = items[i] 69 if searchWord.contains(searchText) { 70 searchResult.append(searchWord) 71 print("searchResult: (searchResult)") 72 } 73 i += 1 74 } 75 print("Filter Succeeded") 76 } catch { 77 print("Filter Failed") 78 } 79 } else { 80 //渡された文字列が空の場合は全てを表示 81 searchResult = items 82 print("All Show") 83 } 84 print("") 85 //tableViewを再読み込みする 86 do { 87 try myTableView.reloadData() 88 os_log("=> \nmyTableView.reloadData()") 89 print("Reload Succeeded") 90 print("") 91 } catch { 92 os_log("=> \nmyTableView.reloadData()") 93 print("Reload Failed") 94 print("") 95 } 96 } 97 98 // MARK: - SearchBar Delegate Methods 99 // テキストが変更される毎に呼ばれる 100 func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { 101 //検索する 102 searchItems(searchText: searchText) 103 } 104 105 // Searchボタンが押されると呼ばれる 106 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { 107 view.endEditing(true) 108 //検索する 109 searchItems(searchText: searchBar.text! as String) 110 } 111 112 // MARK: - TableView Delegate Methods 113 114 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 115 os_log("=> \ntableView") 116 print("tableView() -> Int") 117 print("") 118 return searchResult.count 119 } 120 121 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 122 os_log("=> \ntableView") 123 print("tableView() -> UITableViewCell") 124 print("") 125 // セルを取得する 126 let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "OrigamiCell", for: indexPath) 127 128 // セルに表示する値を設定する 129 cell.textLabel!.text = searchResult[indexPath.row] 130 cell.textLabel!.font = UIFont(name: "system", size: 28) 131 132 return cell 133 } 134 135 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 136 self.index = searchResult[Int(indexPath.row)] 137 self.performSegue(withIdentifier: "OrigamiView", sender: nil) 138 } 139 140 // MARK: - prepare 141 142 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 143 if segue.identifier == "OrigamiView" { 144 if let origamiView: OrigamiView = segue.destination as? OrigamiView { 145 origamiView.image = UIImage(imageLiteralResourceName: self.index! + ".png") 146 origamiView.titleText = self.index! 147 os_log("=> \nSegue to OrigamiVC") 148 print("(self.index!) selected") 149 print("titleText: (self.index!)") 150 print("image: (self.index!).png") 151 print("") 152 } 153 } 154 } 155 156} 157 158

開発環境

MacOS Catalina 10.15.4
Xcode11.4
Swift5

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

'[<UIViewController 0x100e1e9a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key favTableView.'

このエラーですが、

[<FavoriteViewController ....

となっていないので、おそらくStoryboardでViewControllerのカスタムクラスを正しく設定できていないのだと思われます。

投稿2020/04/20 06:15

MasakiHori

総合スコア3384

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shuntar30

2020/04/20 06:18

回答ありがとうございます。
shuntar30

2020/04/20 06:54

確認したところ、クラスはFavoriteViewControllerなっていたので、別の理由だと思います。
shuntar30

2020/04/20 07:01 編集

あと、UIViewControllerなので、クラス名ではないと思います。
guest

0

ベストアンサー

Storyboardでのセグエの設定の仕方

上記のリンクで、コード上でのperformで画面遷移するためには、Storyboard上での遷移元のUIViewControllerの上の左の〇のアイコンから、画面遷移先へCtrlを押しながらSegueを作らないとダメでだと思うのですが、それをしていないとかではないでしょうか。

投稿2020/04/20 12:46

freemann

総合スコア264

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shuntar30

2020/04/21 04:34

ありがとうございます。解決できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問