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

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

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

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

Q&A

解決済

1回答

252閲覧

swift 配列外を参照しているみたいですがわかりません

ssh_u

総合スコア34

Swift

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

0グッド

0クリップ

投稿2017/09/16 09:58

###前提・実現したいこと
swiftで課題リストを作ってます

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

fatal error: Index out of range //100行目 エラーの内容はわかるのですがなぜなっているのかがわかりません どうしたらいいのでしょうか?

###該当のソースコード

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 todocontent = [String]() 9 var refresher: UIRefreshControl? 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 if UserDefaults.standard.array(forKey: "todoarray") == nil { 14 todoarray = [String]() 15 UserDefaults.standard.set(todoarray, forKey: "todoarray") 16 } else { 17 todoarray = UserDefaults.standard.array(forKey: "todoarray") as! [String] 18 } 19 20 if UserDefaults.standard.array(forKey: "todoarraydate") == nil { 21 todoarraydate = [String]() 22 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 23 } else { 24 todoarraydate = UserDefaults.standard.array(forKey: "todoarraydate") as! [String] 25 } 26 27 if UserDefaults.standard.array(forKey: "todocontent") == nil { 28 todocontent = [String]() 29 UserDefaults.standard.set(todocontent, forKey: "todocontent") 30 } else { 31 todocontent = UserDefaults.standard.array(forKey: "todocontent") as! [String] 32 } 33 34 refresher = UIRefreshControl() 35 36 refresher?.attributedTitle = NSAttributedString(string: "Pull to refresh") 37 38 refresher?.addTarget(self, action: #selector(FirstViewController.refresh), for: UIControlEvents.valueChanged) 39 40 self.tableView.addSubview(refresher!) 41 42 refresh() 43 // Do any additional setup after loading the view, typically from a nib. 44 } 45 46 override func didReceiveMemoryWarning() { 47 super.didReceiveMemoryWarning() 48 // Dispose of any resources that can be recreated. 49 } 50 51 internal func numberOfSections(in tableView: UITableView) -> Int { 52 return 1 53 } 54 55 func refresh() { 56 tableView.reloadData() 57 refresher?.endRefreshing() 58 } 59 60 internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 61 if todoarray.count == todoarraydate.count { 62 return todoarray.count 63 } else { 64 return todoarraydate.count 65 } 66 } 67 68 internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 69 var cell = tableView.dequeueReusableCell(withIdentifier: "cell") 70 if cell == nil { 71 cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 72 } 73 cell?.textLabel?.text = todoarray[indexPath.row] 74 cell?.detailTextLabel?.text = String(describing: todoarraydate[indexPath.row]) 75 return cell! 76 } 77 78 internal func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 79 // Return false if you do not want the specified item to be editable. 80 return true 81 } 82 83 @IBAction func new(_ sender: AnyObject) { 84 self.performSegue(withIdentifier: "New To Do", sender: self) 85 } 86 87 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 88 if editingStyle == .delete { 89 todoarray.remove(at: indexPath.row) 90 todoarraydate.remove(at: indexPath.row) 91 print("a") 92100 todocontent.remove(at: indexPath.row) 93 print("b") 94 tableView.deleteRows(at: [indexPath], with: .fade) 95 UserDefaults.standard.set(todoarray, forKey: "todoarray") 96 UserDefaults.standard.set(todoarraydate, forKey: "todoarraydate") 97 UserDefaults.standard.set(todocontent, forKey: "todocontent") 98 } 99 } 100 101 //画面遷移 102 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 103 104 performSegue(withIdentifier: "toSubViewController", sender: nil) 105 } 106 107} 108 109

###試したこと

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

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

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

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

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

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

Stripe

2017/09/16 11:00

todoarrayとtodoarraydateとtodocontentのcountは、必ず同じ数になるんですか?
ssh_u

2017/09/16 11:00

要素数は同じです
Stripe

2017/09/16 11:15

要素数が同じであることをどうやって保証していますか? また、「if todoarray.count == todoarraydate.count {...}else{...}」このコードの意味は何ですか?
ssh_u

2017/09/16 11:22

一応やっているだけであってreturn todoarray.countでいいです
ssh_u

2017/09/16 11:46

すみません。動きました。
fuzzball

2017/09/19 04:49

解決したのであれば、解決した方法を書いて質問をcloseして下さい。
guest

回答1

0

自己解決

シミュレーターの方のアプリを一度消したら治りました

投稿2017/09/19 12:55

ssh_u

総合スコア34

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.51%

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

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

質問する

関連した質問