###前提・実現したいこと
課題期限通知アプリを作りたいです
###発生している問題・エラーメッセージ
Could not cast value of type '__NSDate' (0x104c92080) to 'NSString' (0x10429dc60).``` viewDidLoadでエラーが起こっているようです
swift
1import UIKit 2 3class FirstViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 4 5 @IBOutlet var tableView: UITableView! 6 var todoarray = [String]() 7 var todoarraydate = [String]() 8 var refresher: UIRefreshControl? 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 if UserDefaults.standard.array(forKey: "todoarray") == nil { 13 todoarray = [String]() 14 UserDefaults.standard.set(todoarray, forKey: "todoarray") 15 } else { 16 todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String] 17 } 18 19 if UserDefaults.standard.array(forKey: "todoarraydate") == nil { 20 todoarraydate = [String]() 21 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 22 } else { 23 todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String] 24 } 25 26 refresher = UIRefreshControl() 27 28 refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh") 29 30 refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged) 31 32 self.tableView.addSubview(refresher!) 33 34 refresh() 35 // Do any additional setup after loading the view, typically from a nib. 36 } 37 38 override func didReceiveMemoryWarning() { 39 super.didReceiveMemoryWarning() 40 // Dispose of any resources that can be recreated. 41 } 42 43 internal func numberOfSections(in tableView: UITableView) -> Int { 44 return 1 45 } 46 47 func refresh() { 48 tableView.reloadData() 49 refresher?.endRefreshing() 50 } 51 52 internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 53 if todoarray.count == todoarraydate.count { 54 return todoarray.count 55 } else { 56 return todoarraydate.count 57 } 58 } 59 60 internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 61 var cell = tableView.dequeueReusableCell(withIdentifier: "cell") 62 if cell == nil { 63 cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 64 } 65 cell?.textLabel?.text = todoarray[indexPath.row] 66 cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row]) 67 return cell! 68 } 69 70 internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 71 // Return false if you do not want the specified item to be editable. 72 return true 73 } 74 75 @IBAction func new(_ sender: AnyObject) { 76 self.performSegue(withIdentifier: "New To Do", sender: self) 77 } 78 79 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 80 if editingStyle == .delete { 81 todoarray.remove(at: indexPath.row) 82 todoarraydate.remove(at: indexPath.row) 83 tableView.deleteRows(at: [indexPath], with: .fade) 84 UserDefaults.standard.set(todoarray, forKey: "todoarray") 85 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 86 } 87 } 88 89 //画面遷移 90 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 91 92 performSegue(withIdentifier: "toSubViewController", sender: nil) 93 } 94 95}
###最後に
分からないことがあれば質問してください
よろしくお願いします
回答1件
あなたの回答
tips
プレビュー