###前提・実現したいこと
初心者のためSwift3でToDoリストの作り方を学習しようとネットで見て作っているのですが、コードを書き終えて、シュミュレーターを起動すると以下のエラーが出て終了してしまいます。
###発生している問題・エラーメッセージ
下のコードの return tasksToShow[taskCategories[sention]]!.count の所にThread 1: EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP.subcode=0×0) とエラーが出てしまいます。
###該当のソースコード
import UIKit import CoreData class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { // MARK: - Properties @IBOutlet weak var taskTableView: UITableView! // MARK: - Properties for table view var tasks:[Task] = [] var tasksToShow:[String:[String]] = ["ToDo":[], "Shopping":[], "Assignment":[]] let taskCategories:[String] = ["Todo", "Shopping", "Assignment"] // MARK: - View Life Cycle override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. taskTableView.dataSource = self taskTableView.delegate = self } override func viewWillAppear(_ animated: Bool) { // CoreDataからデータをfetchしてくる getData() // taskTableViewを再読み込みする taskTableView.reloadData() } // MARK: - Method of Getting data from Core Data func getData() { // データ保存じと同様にcontextを定義 let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext do { // CoreDataからデータをfetchしてtasksに格納 let fetchRequest: NSFetchRequest<Task> = Task.fetchRequest() tasks = try context.fetch(fetchRequest) // tasksToShow配列を空にする。(同じデータを複数表示しないため) for key in tasksToShow.keys { tasksToShow[key] = [] } // 先ほどfetchしたデータをtasksToShow配列に格納する for task in tasks { tasksToShow[task.category!]?.append(task.name!) } } catch { print("Fetching Failed") } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // MARK: - Table View Data Source // taskCategories[]に格納されいているTableViewのセクションになる func numberOfSections(in tableView: UITableView) -> Int { return taskCategories.count } func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return taskCategories[section] } // taskToShowにカテゴリー(taskToShowのキーとなっている)ごとのnameが格納されいている func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tasksToShow[taskCategories[section]]!.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() let sectionData = tasksToShow[taskCategories[indexPath.section]] let cellData = sectionData?[indexPath.row] cell.textLabel?.text = "\(String(describing: cellData))" return cell } }
###試したこと
エラーを検索したり数時間前から解決を試みているのですが、解決がしません。ご教授お願いします。
###補足情報(言語/FW/ツール等のバージョンなど)
Swift3 Xcode8.3.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/06/15 08:59
2017/06/20 09:24