質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1494閲覧

swift エラーメッセージ

ssh_u

総合スコア34

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/09/14 12:46

編集2017/09/14 14:29

###前提・実現したいこと
課題期限通知アプリを作りたいです

###発生している問題・エラーメッセージ

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}

###最後に
分からないことがあれば質問してください
よろしくお願いします

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NCC1701

2017/09/14 13:38

どこの行でエラーが発生したのくらい、記載してください。コード全部読むの大変です。
ssh_u

2017/09/14 14:31

swift初心者なのでエラーがどのようなものかわかりませんので、エラーメッセージが乗った画像を貼りましたのでよろしくお願いします。
guest

回答1

0

ベストアンサー

ご提示のソースだけでははっきりとはわかりませんがおそらくUserDefaultsのキーtodoarraydateに他の部分で[Date]を登録しているのだと思います。

force unwrap (!)は極力使わないようにしましょう。

投稿2017/09/14 15:52

MasakiHori

総合スコア3384

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問