teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

修正

2019/01/09 09:23

投稿

hahum
hahum

スコア65

title CHANGED
@@ -1,1 +1,1 @@
1
- cannot use instance member '配列名' within property initializer;と追記
1
+ Thread 1: signal SIGABRT
body CHANGED
@@ -1,48 +1,60 @@
1
- ボタンを押したときにViewControllerの値をUserDefautsの保存して次の画面のTableViewControllerに録として残し、同時にTableViewのセルのSubtitleにその時の時間をつけたいのですが、以下のようなエラーが出てしまいます。
1
+  1/9
2
2
 
3
+ himeji001さんのアドバイス通りやってみたところ
4
+ Array1には回数の入ったresultArray1と日付が入ったdateArray1を入れることが出来ました。
5
+
6
+ しかしSaveViewControllerのViewDidLoadで,取り出しArray2に代入して、cellForRowAtにresultArray2とdateArray2のIndexPath.rowを表示させることがまだ出来ません。(Thread 1: Fatal error: Index out of range)
7
+ そもそも、Array2 = [resultArray2, dateArray2]なのでArray2[IndexPath.row]はわかりますが、その中の配列resultArray2、dateArray2に[IndexPath.row]は使えないですよね。それぞれ新しく更新されたデータをcellForRowAtで
8
+ cell.textLabel!.text (resultArray2の値)
9
+ cell.detailTextLabel!.text(dateArray2の値)
10
+ を入れる方法はありますでしょうか。
11
+ あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
12
+
13
+ そしてスッキリした分かりやすいコードを書くために些細なことでもいいのでアドバイスいただけたら幸いです。
14
+
15
+
16
+
3
17
  ResultViewController.swift
4
- cannot use instance member 'resultArray1' within property initializer; property initializers run before 'self' is available
5
18
 
6
- SaveViewController.swift
19
+ ```Swift
7
- cannot use instance member 'resultArray2' within property initializer; property initializers run before 'self' is available
8
20
 
9
- ...
10
- ...
11
- ...
12
- ResultViewController.swift
13
- ```swift
14
- var resultArray1 = [String]()
21
+ var resultArray1 = [String]()
15
22
 
16
23
  var dateArray1 = [String]()
17
24
 
18
- var Array1 = [resultArray1,dateArray1]
25
+ var Array1: [[String]] = []
19
-
26
+
20
27
  var date = Date()
21
28
 
29
+
22
30
  ...
23
31
  ...
24
32
  ...
25
33
 
26
-
27
-
28
34
  @IBAction func record(_ sender: Any) {
29
35
 
30
- //zikanを呼び出す
31
- zikan()
32
-
33
36
  //配列を取り出す
34
37
  if (UserDefaults.standard.object(forKey: "save") != nil) {
35
- Array1 = UserDefaults.standard.object(forKey: "save") as! [String]
38
+ Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]]
39
+
36
40
  }
41
+
37
42
 
43
+ //zikanを呼び出す
44
+ zikan()
45
+ //resultArrayに回数を追加
46
+ resultArray1.append(countLabel2.text!)
38
47
 
48
+ Array1 = [resultArray1, dateArray1]
39
49
 
40
- resultArray1.append(countLabel2.text!)
41
- /*//日付をdateArrayに入れる
42
- dateArray1.append(sDate)*/
43
50
  UserDefaults.standard.set(Array1, forKey: "save")
51
+
52
+ print(Array1.description)
53
+ print(resultArray1.description)
54
+ print(dateArray1.description)
44
55
  }
45
56
 
57
+
46
58
  func zikan() {
47
59
  //日付のフォーマットを指定する
48
60
  let format = DateFormatter()
@@ -50,35 +62,56 @@
50
62
 
51
63
  //日付をStringに変換する
52
64
  let sDate = format.string(from: date)
53
-
54
65
  //日付をdateArrayに入れる
55
66
  dateArray1.append(sDate)
56
67
  }
57
68
  ```
58
69
 
70
+
59
71
  SaveViewController.swift
72
+
60
- ```Swift
73
+ ```swift
61
74
  var resultArray2 = [String]()
75
+
62
76
  var dateArray2 = [String]()
63
77
 
64
- var Array2 = [resultArray2, dateArray2]
78
+ var Array2: [[String]] = []
65
- //現在の日付を取得
79
+
66
- let date = Date()
80
+ ...
81
+ ...
82
+ ...
67
83
 
68
- ....
69
- ....
70
- ....
71
84
 
72
- override func viewWillAppear(_ animated: Bool) {
85
+ override func viewDidLoad() {
73
- super.viewDidAppear(animated)
86
+ super.viewDidLoad()
87
+
88
+ tableView.delegate = self
89
+ tableView.dataSource = self
90
+
91
+ //Array2にresultArray2とdateArray2を代入
92
+ Array2 = [resultArray2, dateArray2]
93
+
94
+ if UserDefaults.standard.object(forKey: "save") != nil {
74
95
 
75
- if UserDefaults.standard.object(forKey: "save") != nil {
96
+ Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]
97
+ }
76
98
 
99
+ print(resultArray2.description)
100
+ print(dateArray2.description)
77
- Array2 = UserDefaults.standard.object(forKey: "save") as! [String]
101
+ print("Array2:", Array2.description)
102
+
103
+
78
104
  }
79
105
 
106
+
107
+ override func viewWillAppear(_ animated: Bool) {
108
+ super.viewDidAppear(animated)
109
+
110
+
111
+
80
112
  tableView.reloadData()
81
113
 
114
+
82
115
  }
83
116
 
84
117
 
@@ -93,8 +126,6 @@
93
126
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
94
127
  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
95
128
 
96
-
97
-
98
129
  cell.textLabel!.text = resultArray2[indexPath.row]
99
130
  cell.detailTextLabel!.text = dateArray2[indexPath.row]
100
131
 
@@ -118,136 +149,70 @@
118
149
 
119
150
  ```
120
151
 
121
- やりたいことはボタンを押したときに回数(セルのTitle)とその時の時間(セルのSubtitleに)をtableViewにつけたいのですがここで立ち止まっています。
122
- 無駄なコードがあるなど些細なことでもよろしいのでアドバイスいただけたら幸いです。
152
+ ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
153
+ ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
154
+ ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・
123
155
 
156
+ 追記1/9
124
157
 
125
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
158
+ Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]のところにThread 1: signal SIGABRTが発生する理由がわかりません。不必要なキャッシュは切ってあります。
126
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
127
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
128
- ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
129
159
 
130
160
 
131
161
 
132
- 追記 1/9
133
-
134
- himeji001さんのアドバイス通りやってみたところ
135
- Array1には回数の入ったresultArray1と日付が入ったdateArray1を入れることが出来ました。
136
-
137
- しかしSaveViewControllerのViewDidLoadで,取り出しArray2に代入して、cellForRowAtにresultArray2とdateArray2のIndexPath.rowを表示させることがまだ出来ません。(Thread 1: Fatal error: Index out of range)
138
- そもそも、Array2 = [resultArray2, dateArray2]なのでArray2[IndexPath.row]はわかりますが、その中の配列resultArray2、dateArray2に[IndexPath.row]は使えないですよね。それぞれ新しく更新されたデータをcellForRowAtで
139
- cell.textLabel!.text (resultArray2の値)
140
- cell.detailTextLabel!.text(dateArray2の値)
141
- を入れる方法はありますでしょうか。
142
- あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
143
-
144
- そしてスッキリした分かりやすいコードを書くために些細なことでもいいのでアドバイスいただけたら幸いです。
145
-
146
-
147
-
148
- ResultViewController.swift
149
-
150
- ```Swift
151
-
152
- var resultArray1 = [String]()
153
-
154
- var dateArray1 = [String]()
155
-
156
- var Array1: [[String]] = []
157
-
158
- var date = Date()
159
-
160
-
161
- ...
162
- ...
163
- ...
164
-
165
- @IBAction func record(_ sender: Any) {
166
-
167
- //配列を取り出す
168
- if (UserDefaults.standard.object(forKey: "save") != nil) {
169
- Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]]
170
-
171
- }
172
-
173
-
174
- //zikanを呼び出す
175
- zikan()
176
- //resultArrayに回数を追加
177
- resultArray1.append(countLabel2.text!)
178
-
179
- Array1 = [resultArray1, dateArray1]
180
-
181
- UserDefaults.standard.set(Array1, forKey: "save")
182
-
183
- print(Array1.description)
184
- print(resultArray1.description)
185
- print(dateArray1.description)
186
- }
187
-
188
-
189
- func zikan() {
190
- //日付のフォーマットを指定する
191
- let format = DateFormatter()
192
- format.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMdkHm", options: 0, locale: Locale(identifier: "ja_JP"))
193
-
194
- //日付をStringに変換する
195
- let sDate = format.string(from: date)
196
- //日付をdateArrayに入れる
197
- dateArray1.append(sDate)
198
- }
199
- ```
200
-
201
-
202
- SaveViewController.swift
203
-
204
162
  ```swift
163
+
205
164
  var resultArray2 = [String]()
206
165
 
207
166
  var dateArray2 = [String]()
208
167
 
209
168
  var Array2: [[String]] = []
210
-
169
+
170
+
211
171
  ...
212
172
  ...
213
173
  ...
214
174
 
215
175
 
176
+
216
- override func viewDidLoad() {
177
+ override func viewDidLoad() {
217
178
  super.viewDidLoad()
218
-
179
+
219
180
  tableView.delegate = self
220
181
  tableView.dataSource = self
221
-
222
- //Array2にresultArray2とdateArray2を代入
182
+
223
- Array2 = [resultArray2, dateArray2]
224
-
225
183
  if UserDefaults.standard.object(forKey: "save") != nil {
226
184
 
227
185
  Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]
228
186
  }
229
187
 
188
+
189
+ self.resultArray2 = self.Array2[0]
190
+ self.dateArray2 = self.Array2[1]
191
+
192
+ Array2 = [resultArray2, dateArray2]
193
+
194
+
195
+ //Array2 = [ ["晴れ", "雨", "雪"],
196
+ //["2019/1/1", "2019/1/2", "2019/1/3"] ]
197
+
198
+ print("Array2:", Array2.description)
230
199
  print(resultArray2.description)
231
200
  print(dateArray2.description)
232
- print("Array2:", Array2.description)
233
201
 
234
-
235
202
  }
236
203
 
237
204
 
238
205
  override func viewWillAppear(_ animated: Bool) {
239
206
  super.viewDidAppear(animated)
240
-
241
-
242
-
207
+
243
208
  tableView.reloadData()
244
-
245
-
246
209
  }
247
210
 
248
211
 
249
212
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
213
+
250
- return Array2.count
214
+ return resultArray2.count
215
+
251
216
  }
252
217
 
253
218
  func numberOfSections(in tableView: UITableView) -> Int {
@@ -257,6 +222,12 @@
257
222
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
258
223
  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
259
224
 
225
+
226
+ print("Array2:at cellForRowAt", Array2.description)
227
+ print(Array2.count)
228
+ print(resultArray2.description)
229
+ print(dateArray2.description)
230
+
260
231
  cell.textLabel!.text = resultArray2[indexPath.row]
261
232
  cell.detailTextLabel!.text = dateArray2[indexPath.row]
262
233
 

4

わか利やすく変更

2019/01/09 09:23

投稿

hahum
hahum

スコア65

title CHANGED
File without changes
body CHANGED
@@ -135,9 +135,12 @@
135
135
  Array1には回数の入ったresultArray1と日付が入ったdateArray1を入れることが出来ました。
136
136
 
137
137
  しかしSaveViewControllerのViewDidLoadで,取り出しArray2に代入して、cellForRowAtにresultArray2とdateArray2のIndexPath.rowを表示させることがまだ出来ません。(Thread 1: Fatal error: Index out of range)
138
+ そもそも、Array2 = [resultArray2, dateArray2]なのでArray2[IndexPath.row]はわかりますが、その中の配列resultArray2、dateArray2に[IndexPath.row]は使えないですよね。それぞれ新しく更新されたデータをcellForRowAtで
139
+ cell.textLabel!.text (resultArray2の値)
140
+ cell.detailTextLabel!.text(dateArray2の値)
141
+ を入れる方法はありますでしょうか。
142
+ あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
138
143
 
139
- Array2にArray1のデータを入れた後、resultArray2, dateArray2にそれぞれ入れることはできますでしょうか、あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
140
-
141
144
  そしてスッキリした分かりやすいコードを書くために些細なことでもいいのでアドバイスいただけたら幸いです。
142
145
 
143
146
 

3

変更

2019/01/09 05:02

投稿

hahum
hahum

スコア65

title CHANGED
@@ -1,1 +1,1 @@
1
- cannot use instance member '配列名' within property initializerと追記
1
+ cannot use instance member '配列名' within property initializer;と追記
body CHANGED
@@ -122,71 +122,64 @@
122
122
  無駄なコードがあるなど些細なことでもよろしいのでアドバイスいただけたら幸いです。
123
123
 
124
124
 
125
- //////////////////////////////////////////////////////////////////////////
126
- /////////////////////////////////////////////////////////////////////////
127
- /////////////////////////////////////////////////////////////////////////
128
- /////////////////////////////////////////////////////////////////////////
129
- **追記**
125
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
126
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
127
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
128
+ ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
130
129
 
131
130
 
132
131
 
133
- Fatal error: Index out of rangeについてです。
132
+ 追記 1/9
134
133
 
135
- SaveVIewController.swiftViewwillAppearにおい
134
+ himeji001さんアドバイス通りやっみたところ
136
- //Array2にresultArray2とdateArray2を代入
137
- Array2 = [resultArray2,dateArray2]
135
+ Array1には回数の入ったresultArray1と日付が入ったdateArray1を入れることが出来ました。
138
-
139
-
140
- print(Array2.count)
141
- print(resultArray2.count)
142
- print(dateArray2.count)
143
136
 
144
- を実行したところ、
145
- 2
146
- 0
147
- 0
148
- た。2理由はArray2 = [resultArray2,dateArray2]なのすが、resultArray2とdateArray2に値が入っていないことが原因かと思われこれを解決する方法を教えて頂きたいです。
137
+ かしSaveViewControllerViewDidLoad,取り出しArray2に代入してcellForRowAtにresultArray2とdateArray2のIndexPath.rowを表示させることがまだ出来ません(Thread 1: Fatal error: Index out of range)
149
138
 
139
+ Array2にArray1のデータを入れた後、resultArray2, dateArray2にそれぞれ入れることはできますでしょうか、あと、ViewWillAppearはreloaddataだけでいいのか教えていただきたいです。
150
140
 
141
+ そしてスッキリした分かりやすいコードを書くために些細なことでもいいのでアドバイスいただけたら幸いです。
142
+
143
+
144
+
151
145
  ResultViewController.swift
146
+
152
147
  ```Swift
148
+
153
149
  var resultArray1 = [String]()
154
150
 
155
151
  var dateArray1 = [String]()
156
152
 
157
153
  var Array1: [[String]] = []
158
-
154
+
159
155
  var date = Date()
160
156
 
161
- ...
162
- ...
163
- ...
164
157
 
165
- override func viewDidLoad() {
166
- super.viewDidLoad()
167
-
168
- Array1 = [resultArray1, dateArray1]
169
158
  ...
170
159
  ...
171
160
  ...
172
-
173
- }
174
- }
175
-
176
- @IBAction func record(_ sender: Any) {
177
161
 
178
-
179
- //zikanを呼び出す
162
+ @IBAction func record(_ sender: Any) {
180
- zikan()
163
+
181
-
182
164
  //配列を取り出す
183
165
  if (UserDefaults.standard.object(forKey: "save") != nil) {
184
166
  Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]]
167
+
185
168
  }
169
+
186
170
 
171
+ //zikanを呼び出す
172
+ zikan()
173
+ //resultArrayに回数を追加
187
174
  resultArray1.append(countLabel2.text!)
188
175
 
176
+ Array1 = [resultArray1, dateArray1]
177
+
189
178
  UserDefaults.standard.set(Array1, forKey: "save")
179
+
180
+ print(Array1.description)
181
+ print(resultArray1.description)
182
+ print(dateArray1.description)
190
183
  }
191
184
 
192
185
 
@@ -197,59 +190,51 @@
197
190
 
198
191
  //日付をStringに変換する
199
192
  let sDate = format.string(from: date)
200
-
201
193
  //日付をdateArrayに入れる
202
194
  dateArray1.append(sDate)
203
-
204
195
  }
205
196
  ```
206
197
 
207
- SaveViewController.Swift
208
- ```Swift
209
- import UIKit
210
198
 
211
- class SaveViewController: UIViewController,UITableViewDelegate,
212
- UITableViewDataSource {
199
+ SaveViewController.swift
213
200
 
201
+ ```swift
214
- @IBOutlet var tableView: UITableView!
202
+ var resultArray2 = [String]()
215
203
 
216
- var resultArray2 = [String]()
217
204
  var dateArray2 = [String]()
218
205
 
219
206
  var Array2: [[String]] = []
220
207
 
221
-
208
+ ...
222
-
209
+ ...
210
+ ...
211
+
212
+
223
- override func viewDidLoad() {
213
+ override func viewDidLoad() {
224
214
  super.viewDidLoad()
225
-
226
215
 
227
-
228
216
  tableView.delegate = self
229
217
  tableView.dataSource = self
230
218
 
219
+ //Array2にresultArray2とdateArray2を代入
220
+ Array2 = [resultArray2, dateArray2]
221
+
222
+ if UserDefaults.standard.object(forKey: "save") != nil {
223
+
224
+ Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]
231
225
  }
232
226
 
227
+ print(resultArray2.description)
228
+ print(dateArray2.description)
229
+ print("Array2:", Array2.description)
233
230
 
234
- override func viewWillAppear(_ animated: Bool) {
235
- super.viewDidAppear(animated)
236
231
 
237
-
238
- if UserDefaults.standard.object(forKey: "save") != nil {
239
-
240
- Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]
241
232
  }
242
233
 
243
234
 
244
- //Array2にresultArray2とdateArray2を代入
245
- Array2 = [resultArray2,dateArray2]
235
+ override func viewWillAppear(_ animated: Bool) {
236
+ super.viewDidAppear(animated)
246
237
 
247
- resultArray2 = Array2[0]
248
- dateArray2 = Array2[1]
249
-
250
- print(Array2.count)
251
- print(resultArray2.count)
252
- print(dateArray2.count)
253
238
 
254
239
 
255
240
  tableView.reloadData()
@@ -269,9 +254,7 @@
269
254
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
270
255
  let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
271
256
 
272
-
273
-
274
- cell.textLabel!.text = resultArray2[indexPath.row] **Fatal error: Index out of range**
257
+ cell.textLabel!.text = resultArray2[indexPath.row]
275
258
  cell.detailTextLabel!.text = dateArray2[indexPath.row]
276
259
 
277
260
  return cell
@@ -292,16 +275,4 @@
292
275
  }
293
276
  }
294
277
 
295
- /*
296
- // MARK: - Navigation
297
-
298
- // In a storyboard-based application, you will often want to do a little preparation before navigation
299
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
300
- // Get the new view controller using segue.destination.
301
- // Pass the selected object to the new view controller.
302
- }
303
- */
304
-
305
- }
306
-
307
278
  ```

2

修正

2019/01/09 04:13

投稿

hahum
hahum

スコア65

title CHANGED
@@ -1,1 +1,1 @@
1
- cannot use instance member '配列名' within property initializer;
1
+ cannot use instance member '配列名' within property initializerと追記
body CHANGED
@@ -244,7 +244,9 @@
244
244
  //Array2にresultArray2とdateArray2を代入
245
245
  Array2 = [resultArray2,dateArray2]
246
246
 
247
-
247
+ resultArray2 = Array2[0]
248
+ dateArray2 = Array2[1]
249
+
248
250
  print(Array2.count)
249
251
  print(resultArray2.count)
250
252
  print(dateArray2.count)

1

追記

2019/01/07 19:52

投稿

hahum
hahum

スコア65

title CHANGED
File without changes
body CHANGED
@@ -119,4 +119,187 @@
119
119
  ```
120
120
 
121
121
  やりたいことはボタンを押したときに回数(セルのTitle)とその時の時間(セルのSubtitleに)をtableViewにつけたいのですがここで立ち止まっています。
122
- 無駄なコードがあるなど些細なことでもよろしいのでアドバイスいただけたら幸いです。
122
+ 無駄なコードがあるなど些細なことでもよろしいのでアドバイスいただけたら幸いです。
123
+
124
+
125
+ //////////////////////////////////////////////////////////////////////////
126
+ /////////////////////////////////////////////////////////////////////////
127
+ /////////////////////////////////////////////////////////////////////////
128
+ /////////////////////////////////////////////////////////////////////////
129
+ **追記**
130
+
131
+
132
+
133
+ Fatal error: Index out of rangeについてです。
134
+
135
+ SaveVIewController.swiftのViewwillAppearにおいて、
136
+ //Array2にresultArray2とdateArray2を代入
137
+ Array2 = [resultArray2,dateArray2]
138
+
139
+
140
+ print(Array2.count)
141
+ print(resultArray2.count)
142
+ print(dateArray2.count)
143
+
144
+ を実行したところ、
145
+ 2
146
+ 0
147
+ 0
148
+ でした。2の理由はArray2 = [resultArray2,dateArray2]なのですが、resultArray2とdateArray2に値が入っていないことが原因かと思われます。これを解決する方法を教えて頂きたいです。
149
+
150
+
151
+ ResultViewController.swift
152
+ ```Swift
153
+ var resultArray1 = [String]()
154
+
155
+ var dateArray1 = [String]()
156
+
157
+ var Array1: [[String]] = []
158
+
159
+ var date = Date()
160
+
161
+ ...
162
+ ...
163
+ ...
164
+
165
+ override func viewDidLoad() {
166
+ super.viewDidLoad()
167
+
168
+ Array1 = [resultArray1, dateArray1]
169
+ ...
170
+ ...
171
+ ...
172
+
173
+ }
174
+ }
175
+
176
+ @IBAction func record(_ sender: Any) {
177
+
178
+
179
+ //zikanを呼び出す
180
+ zikan()
181
+
182
+ //配列を取り出す
183
+ if (UserDefaults.standard.object(forKey: "save") != nil) {
184
+ Array1 = UserDefaults.standard.object(forKey: "save") as! [[String]]
185
+ }
186
+
187
+ resultArray1.append(countLabel2.text!)
188
+
189
+ UserDefaults.standard.set(Array1, forKey: "save")
190
+ }
191
+
192
+
193
+ func zikan() {
194
+ //日付のフォーマットを指定する
195
+ let format = DateFormatter()
196
+ format.dateFormat = DateFormatter.dateFormat(fromTemplate: "yMdkHm", options: 0, locale: Locale(identifier: "ja_JP"))
197
+
198
+ //日付をStringに変換する
199
+ let sDate = format.string(from: date)
200
+
201
+ //日付をdateArrayに入れる
202
+ dateArray1.append(sDate)
203
+
204
+ }
205
+ ```
206
+
207
+ SaveViewController.Swift
208
+ ```Swift
209
+ import UIKit
210
+
211
+ class SaveViewController: UIViewController,UITableViewDelegate,
212
+ UITableViewDataSource {
213
+
214
+ @IBOutlet var tableView: UITableView!
215
+
216
+ var resultArray2 = [String]()
217
+ var dateArray2 = [String]()
218
+
219
+ var Array2: [[String]] = []
220
+
221
+
222
+
223
+ override func viewDidLoad() {
224
+ super.viewDidLoad()
225
+
226
+
227
+
228
+ tableView.delegate = self
229
+ tableView.dataSource = self
230
+
231
+ }
232
+
233
+
234
+ override func viewWillAppear(_ animated: Bool) {
235
+ super.viewDidAppear(animated)
236
+
237
+
238
+ if UserDefaults.standard.object(forKey: "save") != nil {
239
+
240
+ Array2 = UserDefaults.standard.object(forKey: "save") as! [[String]]
241
+ }
242
+
243
+
244
+ //Array2にresultArray2とdateArray2を代入
245
+ Array2 = [resultArray2,dateArray2]
246
+
247
+
248
+ print(Array2.count)
249
+ print(resultArray2.count)
250
+ print(dateArray2.count)
251
+
252
+
253
+ tableView.reloadData()
254
+
255
+
256
+ }
257
+
258
+
259
+ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
260
+ return Array2.count
261
+ }
262
+
263
+ func numberOfSections(in tableView: UITableView) -> Int {
264
+ return 1
265
+ }
266
+
267
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
268
+ let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
269
+
270
+
271
+
272
+ cell.textLabel!.text = resultArray2[indexPath.row] **Fatal error: Index out of range**
273
+ cell.detailTextLabel!.text = dateArray2[indexPath.row]
274
+
275
+ return cell
276
+ }
277
+
278
+ func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
279
+ return 40
280
+ }
281
+
282
+ func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
283
+
284
+ if editingStyle == .delete {
285
+ Array2.remove(at: indexPath.row)
286
+
287
+ UserDefaults.standard.set(Array2, forKey: "save")
288
+
289
+ tableView.reloadData()
290
+ }
291
+ }
292
+
293
+ /*
294
+ // MARK: - Navigation
295
+
296
+ // In a storyboard-based application, you will often want to do a little preparation before navigation
297
+ override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
298
+ // Get the new view controller using segue.destination.
299
+ // Pass the selected object to the new view controller.
300
+ }
301
+ */
302
+
303
+ }
304
+
305
+ ```