前提・実現したいこと
健康管理アプリを作っています。
writeViewControllerで保存したDouble型の数値配列データを、ViewControllerを介して、healthViewControllerに渡したいです。prepareforSegueを利用して、遷移時に数値を渡そうとしているのですが、「Thread 1: signal SIGABRT」となり、prepareforSegueのコードで止められてしまいます。どのようにして値を渡すのが良いのでしょうか?具体的に教えていただけると嬉しいです。
該当のソースコード(ViewController)
Swift
1class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 2 3 var healthNumber1 = [Double]() 4 5 var heinetuDate = [Double]() 6 7 var todayDate = [String]() 8 9 @IBOutlet weak var dateTableView : UITableView! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 let nib = UINib(nibName: "dateTableViewCell", bundle: nil) 15 dateTableView.register(nib, forCellReuseIdentifier: "dateTableViewCell") 16 17 // yyyy年MM月01日 12時 18 let lastDate = DateComponents(calendar: Calendar.current, day: 1, hour: 12).date! 19 20 // yyyy年MM月15日 11時 21 let now = DateComponents(calendar: Calendar.current, day: 15, hour: 11).date! 22 23 let lastDateStartOfDay = Calendar.current.startOfDay(for: lastDate) 24 let nowStartOfDay = Calendar.current.startOfDay(for: now) 25 26 let elapsedDays = Calendar.current.dateComponents([.day], from: lastDateStartOfDay, to: nowStartOfDay).day! 27 28 if elapsedDays > 14 { 29 30 todayDate.removeAll() 31 healthNumber1.removeAll() 32 33 UserDefaults.standard.set(healthNumber1, forKey: "HealthNumber1") 34 UserDefaults.standard.set(todayDate, forKey: "TodayDate") 35 } 36 37 } 38 39 override func viewDidAppear(_ animated: Bool) { 40 self.dateTableView.reloadData() 41 } 42 43 override func viewWillAppear(_ animated: Bool) { 44 super.viewWillAppear(animated) 45 46 if UserDefaults.standard.object(forKey: "HealthNumber1") != nil { 47 healthNumber1 = UserDefaults.standard.object(forKey: "HealthNumber1") as! [Double] 48 } 49 50 if UserDefaults.standard.object(forKey: "HeinetuDate") != nil { 51 heinetuDate = UserDefaults.standard.object(forKey: "HeinetuDate") as! [Double] 52 } 53 54 if UserDefaults.standard.object(forKey: "TodayDate") != nil { 55 todayDate = UserDefaults.standard.object(forKey: "TodayDate") as! [String] 56 } 57 58 } 59 60 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 61 if segue.identifier == "goThird" { 62 63 let thirdVc = segue.destination as! healthViewController 64 // 値を渡す 65 thirdVc.numberRow = Double((sender as! IndexPath).row) 66 67 } 68 69 } 70 71 72 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 73 74 return todayDate.count 75 } 76 77 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 78 79 let dateCell = tableView.dequeueReusableCell(withIdentifier: "dateTableViewCell") as! dateTableViewCell 80 81 dateCell.date2Label.text = todayDate[indexPath.row] 82 83 if UserDefaults.standard.object(forKey: "HealthNumber1") != nil { 84 let bodyNum = UserDefaults.standard.object(forKey: "HealthNumber1") as! [Double] 85 86 let strf: [String] = bodyNum.map{NSString(format: "%.1f", $0) as String} 87 88 dateCell.health2Label.text = strf[indexPath.row] 89 90 } 91 92 93 94 95 return dateCell 96 } 97 98
該当のソースコード(writeViewController)
Swift
1class writeViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource { 2 3 var healthNumber1 = [Double]() 4 5 var todayDate = [String]() 6 7 @IBOutlet weak var outputLabel1 : UILabel! 8 @IBOutlet weak var decideButton : UIButton! 9 @IBOutlet weak var healthPickerView: UIPickerView! 10 @IBOutlet weak var smellSwitch : UISwitch! 11 @IBOutlet weak var tasteSwitch : UISwitch! 12 @IBOutlet weak var dateLabel : UILabel! 13 14 var smellNumber :Int = 0 15 var tasteNumber :Int = 0 16 17// var outputdate : Double = 0.0 18 19 let list1 : [Double] = [35.0,35.1,35.2,35.3,35.4,35.5,35.6,35.7,35.8,35.9,36.0,36.1,36.2,36.3,36.4,36.5,36.6,36.7,36.8,36.9,37.0,37.1,37.2,37.3,37.4,37.5,37.6,37.7,37.8,37.9,38.0,38.1,38.2,38.3,38.4,38.5,38.6,38.7,38.8,38.9,39.0,40.0,40.1,40.2,40.3,40.4,40.5,40.6,40.7,40.8,40.9,41.0] 20 21 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 25 if UserDefaults.standard.object(forKey: "HealthNumber1") != nil { 26 healthNumber1 = UserDefaults.standard.object(forKey: "HealthNumber1") as! [Double] 27 } 28 29 if UserDefaults.standard.object(forKey: "TodayDate") != nil { 30 todayDate = UserDefaults.standard.object(forKey: "TodayDate") as! [String] 31 } 32 33 if #available(iOS 13.0, *) { 34 self.overrideUserInterfaceStyle = .light 35 } else { 36 // Fallback on earlier versions 37 } 38 39 //DateFormatterクラスのインスタンス生成 40 let dateFormatter = DateFormatter() 41 //ロケールの設定(日本・東京) 42 dateFormatter.locale = Locale(identifier: "ja_JP") 43 //スタイルの設定 44 dateFormatter.dateStyle = .medium 45 46 let now = Date() 47 dateLabel.text = "(dateFormatter.string(from: now))" 48 49 50 } 51 52 //PickerViewの設定 53 func numberOfComponents(in pickerView: UIPickerView) -> Int { 54 55 return 1 56 } 57 58 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 59 60 return list1.count 61 62 } 63 64 // ドラムロールの各タイトル 65 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 66 67 return String(format: "%.1f", list1[row]) 68 } 69 70 71 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 72 73 let output = list1[pickerView.selectedRow(inComponent: 0)] 74 75 outputLabel1.text = "(output)" 76 } 77 78 //ボタンの設定 79 @IBAction func TodoAddButten(_ sender: Any) { 80 81 let isOn = smellSwitch.isOn 82 if isOn == true { 83 smellNumber = 1 84 } 85 86 let isOn2 = tasteSwitch.isOn 87 if isOn2 == true { 88 tasteNumber = 1 89 } 90 91 healthNumber1.append(Double(outputLabel1.text!) ?? -1) 92 93 todayDate.append(dateLabel.text!) 94 95 // 追加ボタンを押したらテキストフィールドを空にする 96 outputLabel1.text = "" 97 98 UserDefaults.standard.set(healthNumber1, forKey: "HealthNumber1") 99 100 UserDefaults.standard.set(todayDate, forKey: "TodayDate") 101 102 UserDefaults.standard.set(smellNumber, forKey: "SmellNumber") 103 104 UserDefaults.standard.set(tasteNumber, forKey: "TasteNumber") 105 106 self.navigationController?.popToRootViewController(animated: true) 107 } 108
該当のソースコード(healthViewController)
Swift
1class healthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 2 3 @IBOutlet weak var healthTableView : UITableView! 4 5 var healthNumber1 = [Double]() 6 7 var heinetuDate = [Double]() 8 9 var numberRow : Double = 0.0 10 11 var heinetuRow : Double = 0.0 12 13 var healthNum : Double = 0.0 14 15 var heinetuNum : Double = 0.0 16 17 18 override func viewDidLoad() { 19 super.viewDidLoad() 20 21 22 if UserDefaults.standard.object(forKey: "HealthNumber1") != nil { 23 let numbers = UserDefaults.standard.object(forKey: "HealthNumber1") as! [Double] 24 25 healthNum = numbers[Int((Double(numberRow)))] 26 27 } 28 29 if UserDefaults.standard.object(forKey: "HeinetuDate") != nil { 30 let heinetuNumber = UserDefaults.standard.object(forKey: "HeinetuDate") as! [Double] 31 32 heinetuNum = heinetuNumber[Int(Double(heinetuRow))] 33 } 34 35
補足情報(FW/ツールのバージョンなど)
Xcodeバージョン12.4
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/02/16 11:13
2021/02/16 11:25
2021/02/16 11:26
2021/02/28 06:34 編集
2021/02/28 13:17
2021/02/28 13:43