ボタンを押したらTableViewに記録が残るアプリをしているのですが、Saveボタンを押すとSigabrtエラーが出てしまします、不必要なキャッシュは切ってあるので、紐付けが原因ではないはずなのですが原因がわからなく困っております。アドバイスいただけたら幸いです。
Swift
1 var resultArray1 = [String]() 2 3 var dateArray1 = [String]() 4 5 var Array1: [[String]] = [] 6 7 var date1 = Date() 8 9... 10... 11... 12 13 14 15 @IBAction func save(_ sender: Any) { 16 17 //配列を取り出す 18 if (UserDefaults.standard.object(forKey: "save") != nil) { 19 Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]] __**Thread 1: signal SIGABRT**__ 20 21 } 22 23 //zikanを呼び出す 24 zikan() 25 //resultArrayに回数を追加 26 resultArray1.append(countLabel2.text!) 27 28 Array1 = [resultArray1, dateArray1] 29 30 //Array1の要素0(resultArray1の保存したもの)をresultArray1に入れる 31 self.resultArray1 = self.Array1[0] 32 // Array1の要素1(dateArray1の保存したもの)をdateArray1に入れる 33 self.dateArray1 = self.Array1[1] 34 35 UserDefaults.standard.set(Array1, forKey: "save") 36 37 print("Array1:", Array1.description) 38 print(resultArray1.description) 39 print(dateArray1.description) 40 } 41 42 43 func zikan() { 44 //日付のフォーマットを指定する 45 let format = DateFormatter() 46 format.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMdkHm", options: 0, locale: Locale(identifier: "ja_JP")) 47 48 //日付をStringに変換する 49 let sDate = format.string(from: date1) 50 //日付をdateArrayに入れる 51 dateArray1.append(sDate) 52 } 53
追記 1/11
新しいエラーが次の画面であるTableViewに出ています。(Sigabrt)
前回同様部品の紐付けは確認いたしました。エラーメッセージは前回同様Could not cast value of type 'NSTaggedPointerString' to 'NSArray' .です。
SaveViewContoroller.swift
swift
1var resultArray2 = [String]() 2 3 var dateArray2 = [String]() 4 5 var Array2: [[String]] = [] 6 ... 7... 8... 9 10 11override func viewDidLoad() { 12 super.viewDidLoad() 13 14 15 tableView.delegate = self 16 tableView.dataSource = self 17 18 if UserDefaults.standard.object(forKey: "save") != nil { 19 20 Array2 = [UserDefaults.standard.object(forKey: "save") as! [String]] 21 } 22 23 Array2 = [resultArray2, dateArray2] 24 25 26 self.resultArray2 = self.Array2[0] 27 self.dateArray2 = self.Array2[1] 28 29 30 31 32 //Array2 = [ ["晴れ", "雨", "雪"], 33 //["2019/1/1", "2019/1/2", "2019/1/3"] ] 34 35 print("Array2:", Array2.description) 36 print(resultArray2.description) 37 print(dateArray2.description) 38 39 } 40 41 42 override func viewWillAppear(_ animated: Bool) { 43 super.viewDidAppear(animated) 44 45 tableView.reloadData() 46 } 47 48 49 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 50 51 return resultArray2.count 52 53 } 54 55 func numberOfSections(in tableView: UITableView) -> Int { 56 return 1 57 } 58 59 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 60 let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 61 62 63 print("Array2:at cellForRowAt", Array2.description) 64 print(Array2.count) 65 print(resultArray2.description) 66 print(dateArray2.description) 67 68 cell.textLabel!.text = resultArray2[indexPath.row] 69 cell.detailTextLabel!.text = dateArray2[indexPath.row] 70 71 return cell 72 } 73 74 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 75 return 40 76 } 77 78 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 79 80 if editingStyle == .delete { 81 Array2.remove(at: indexPath.row) 82 83 UserDefaults.standard.set(Array2, forKey: "save") 84 85 tableView.reloadData() 86 } 87 } 88
回答2件
あなたの回答
tips
プレビュー