Todoアプリを作って、シミュレータを起動したらエラーが出て困っています。
エラーは、
AppDelegate
1class AppDelegate: UIResponder, UIApplicationDelegate { //Thread 1: signal SIGABRT
で発生しました。
どうか、お解決方法を教えてください。
import UIKit class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource { @IBOutlet weak var tableView: UITableView! //セルの配列 var result_array = [String]() override func viewDidLoad() { super.viewDidLoad() //デリゲートを取得 tableView.delegate = self tableView.dataSource = self } override func viewWillAppear(_ animated: Bool) { //配列に保存したセルを格納する if UserDefaults.standard.object(forKey: "array") != nil { result_array = UserDefaults.standard.object(forKey: "array") as! [String] } //セル数、セクション数、セルを呼び出す、を実行する tableView.reloadData() } func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return result_array.count } //セルが生成されたら実行 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { //セルに追加したタスクを格納 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) //タスクをセルのテキスト欄に追加 cell.textLabel?.text = result_array[indexPath.row] return cell } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { //スワイプしたセルを消去 result_array.remove(at: indexPath.row) //セルを保存 UserDefaults.standard.set(result_array, forKey: "array") //テーブルビューを更新 tableView.reloadData() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
TodoViewController
1i![イメージ説明](5c0de63fe388c1e753403d5ad2bd7eea.png)UIKit 2 3class TodoViewController: UIViewController { 4 5 //配列 6 var array = [String]() 7 8 @IBOutlet weak var textField: UITextField! 9 10 @IBAction func addTodo(_ sender: Any) { 11 12 //一旦、現在の配列状況を保存する 13 if UserDefaults.standard.object(forKey: "array") != nil { 14 array = UserDefaults.standard.object(forKey: "array") as! [String] 15 } 16 17 //入力値を配列に追加 18 array.append(textField.text!) 19 20 //タスクを追加 21 UserDefaults.standard.set(array, forKey: "array") 22 23 //前画面に戻る 24 self.navigationController?.popViewController(animated: true) 25 26 } 27 28 29 override func viewDidLoad() { 30 super.viewDidLoad() 31 32 33 34 // Do any additional setup after loading the view. 35 } 36 37 override func didReceiveMemoryWarning() { 38 super.didReceiveMemoryWarning() 39 // Dispose of any resources that can be recreated. 40 } 41 42 43 /* 44 // MARK: - Navigation 45 46 // In a storyboard-based application, you will often want to do a little preparation before navigation 47 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 48 // Get the new view controller using segue.destinationViewController. 49 // Pass the selected object to the new view controller. 50 } 51 */ 52 53} 54
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。