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

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

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

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

Q&A

解決済

1回答

3672閲覧

Index out of rangeが表示される

kackey621

総合スコア18

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Swift

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

0グッド

0クリップ

投稿2019/05/02 10:05

編集2019/05/02 14:51

Firebaseのデータベースからテーブルビューで取得したタップしたコンテンツのtext1を
"watasuData"に代入したいと考えています。
テーブルビュー
例えば、2をタップしたとします。
この時、下記の通り示した通り、2が含んでいるtext1のデータは、"sklad"になります。
このデータをwatasuDataに代入したいと考えています。

Firebaseのデータ構造図

そこで、一旦2が含んでいる,text1,text2のデータをname2の配列に代入しました。
ここで、name2の1番目の値を取り出すようにコードを書き、
取り出そうとしましたが、Index out of rangeのエラーが出てしまい、
取り出せずにいます。配列がしっかりと入っているはずと認識しております。

Swift4

1import UIKit 2import Firebase 3import FirebaseDatabase 4 5class TableViewController: UITableViewController { 6 7 var ref: DatabaseReference! 8 var databaseHandle:DatabaseHandle? 9 var databaseHandle1:DatabaseHandle? 10 11 //cellのlabelに代入するString 12// var name1:[String] = ["TEST1","TEST2","TEST3","TEST4"] 13// var name2:[String] = ["1","2","3","4"] 14// 15 //TableViewです取得する配列を宣言する 16 var name1 = [String]() 17 var name2 = [String]() 18 19 20 21 override func viewDidLoad() { 22 super.viewDidLoad() 23 24 25 // Uncomment the following line to preserve selection between presentations 26 // self.clearsSelectionOnViewWillAppear = false 27 28 //リファレンスをセットする 29 ref = Database.database().reference() 30// databaseHandle1 = ref.child("TestApp(giveData!)") 31 databaseHandle = ref.child("TestApp").observe(.childAdded, with: ({ (Snapshot) in 32 //childが追加された時の動作を記入する 33 //データをSnapshotから取得して配列に代入する 34 35 let post = Snapshot.key as? String 36 37 if let actualPost = post{ 38 //データをアペンド(作成済みのデータに追加する) 39 self.name1.append(actualPost) 40// self.name2.append(actualPost) 41 //テーブルビューを再度読み込む 42 self.tableView.reloadData() 43 44 print(actualPost) 45 46 } 47 48 })) 49// 50 } 51 52 //遷移先のViewControllerに渡す変数 53 var giveData: String = "" 54 var giveData2:String = "" 55 var watasuData:String = "" 56 57 //sectionの数を返す数 58 override func numberOfSections(in tableView: UITableView) -> Int { 59 return 1 60 } 61 62 // sectionの数を返す関数乗せる 63 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 64 if section == 0{ 65 return name1.count 66 67// }else if section == 1{ 68// return name2.count 69// 70 }else{ 71 return 0 72 73 } 74 } 75 76 //sectionの高さを返す 77 override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 78 return 40 79 } 80 81 //sectionに載せる文字列を返す関数 82 override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 83 return "section(section)" 84 } 85 86 //cellの情報を書き込む関数 87 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 88 let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell 89 90 //ここでcellのlabelに値を入れる 91 if indexPath.section == 0{ 92 cell.name.text = name1[indexPath.item] 93 94// }else{ 95// cell.name.text = name2[indexPath.item] 96 } 97 98 return cell 99 } 100 101 // cellが押されたときに呼ばれる関数 102 // 画面遷移の処理もここで書いている 103 104 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 105 //押された時のcellのlabelの文字列をViewControllerに渡したいので、giveDataに入れる 106 107 if indexPath.section == 0{ 108 giveData = name1[indexPath.item] 109// giveData2 = name2[indexPath.item] 110 print(giveData) 111 } 112 print(indexPath) 113 114 115 //リファレンスをセットする 116 ref = Database.database().reference() 117 118 databaseHandle1 = ref.child("TestApp").child("(giveData)").observe(.childAdded, with: ({ (Snapshot) in 119 //childが追加された時の動作を記入する 120 //データをSnapshotから取得して配列に代入する 121 let post1 = Snapshot.value as? String 122 if let actualPost = post1{ 123 //データをアペンド(作成済みのデータに追加する) 124 self.name2.append(actualPost) 125 // self.name2.append(actualPost) 126 127// var watasuData = self.name2 128 129 print("(self.name2)+1") 130 131 132 if indexPath.section == 0{ 133 self.watasuData = self.name2[1] 134 135 } 136 137 138// print("(self.name2)===YESY") 139 140 } 141 142 })) 143 144 145 // else{ 146// 147// } 148 149 150 151 //Segueを使った画面遷移をおこな関数 152 performSegue(withIdentifier: "Segue", sender: nil) 153 154 155 } 156 157 //遷移先のViewControllerにデータを渡す関数 158 159 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 160 if segue.identifier == "Segue"{ 161 162 163 let vc = segue.destination as! ViewController 164 165 vc.receiveData = giveData 166 vc.receiveData2 = watasuData 167 } 168 } 169 170 171 172 // MARK: - Table view data source 173 174// override func numberOfSections(in tableView: UITableView) -> Int { 175// // #warning Incomplete implementation, return the number of sections 176// return 0 177// } 178// 179// override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 180// // #warning Incomplete implementation, return the number of rows 181// return 0 182// } 183 184 /* 185 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 186 let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 187 188 // Configure the cell... 189 190 return cell 191 } 192 */ 193 194 /* 195 // Override to support conditional editing of the table view. 196 override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 197 // Return false if you do not want the specified item to be editable. 198 return true 199 } 200 */ 201 202 /* 203 // Override to support editing the table view. 204 override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { 205 if editingStyle == .delete { 206 // Delete the row from the data source 207 tableView.deleteRows(at: [indexPath], with: .fade) 208 } else if editingStyle == .insert { 209 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 210 } 211 } 212 */ 213 214 /* 215 // Override to support rearranging the table view. 216 override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) { 217 218 } 219 */ 220 221 /* 222 // Override to support conditional rearranging of the table view. 223 override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { 224 // Return false if you do not want the item to be re-orderable. 225 return true 226 } 227 */ 228 229 /* 230 // MARK: - Navigation 231 232 // In a storyboard-based application, you will often want to do a little preparation before navigation 233 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 234 // Get the new view controller using segue.destination. 235 // Pass the selected object to the new view controller. 236 } 237 */ 238 239 240 241 242 243 244 245 246} 247 248 249

エラー文は下記の通りです。

Swift

12019-05-02 23:38:44.593693+0900 TEST[76364:3624471] TIC Read Status [1:0x0]: 1:57 2-LdsmjzK2nXOotWp07js 3-Ldsqg4BVsm3-D7qNDnF 4-LdsqkSX5Ky7nTGIhuHG 5-LdsqlxFYDEEwIKXTi9L 6-LdsqlxFYDEEwIKXTi9L 7[0, 3] 8["vrt"]+1 9Fatal error: Index out of range 102019-05-02 23:38:48.546084+0900 TEST[76364:3624363] Fatal error: Index out of range 11

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

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

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

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

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

guest

回答1

0

ベストアンサー

Index out of rangeのエラーは、対外添え字範囲外のエラーですよ。添え字(インデックス)がどんな値になっているか確認してみてください

投稿2019/05/02 11:37

akirafudo6

総合スコア341

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

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

kackey621

2019/05/02 12:55

indexの部分を1にしても、 治りませんでした。
kackey621

2019/05/02 12:55

正しく書くと、 配列部分で読み出す部分を1に指定しました。
kackey621

2019/05/02 14:43

能力が追いついていない為、具体的な部分をご指摘いただけると理解することができると思います。お手数ですが、追記していただけると幸いです。
y_waiwai

2019/05/02 15:23

1にしてもダメなら、その配列は要素が1以下ってことです 配列に何が入ってるのかみてみよう
akirafudo6

2019/07/29 07:56

name2の配列初期化してないのでは? なのでapendしても何も入らない!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問