質問編集履歴

1

質問を分けました。

2020/03/31 22:39

投稿

pegy
pegy

スコア243

test CHANGED
File without changes
test CHANGED
@@ -20,7 +20,7 @@
20
20
 
21
21
 
22
22
 
23
- let settingKey = "timer_value" //ここです
23
+ let settingKey = "timer_value" //ここです
24
24
 
25
25
  の定数の宣言なのですが、ここでsettingKeyの定数に文字列の"timer_value"を代入することに何か意味があるのでしょうか?
26
26
 
@@ -30,228 +30,214 @@
30
30
 
31
31
 
32
32
 
33
+
34
+
35
+
36
+
37
+ ```Swift
38
+
39
+
40
+
41
+ import UIKit
42
+
43
+
44
+
45
+ class ViewController: UIViewController {
46
+
47
+
48
+
49
+ var timer : Timer?
50
+
51
+ var count = 0
52
+
53
+ let settingKey = "timer_value" //ここです
54
+
55
+
56
+
57
+ override func viewDidLoad() {
58
+
59
+ super.viewDidLoad()
60
+
61
+ // Do any additional setup after loading the view.
62
+
63
+ let settings = UserDefaults.standard
64
+
65
+ settings.register(defaults: [settingKey:10])
66
+
67
+ }
68
+
69
+
70
+
71
+ @IBOutlet weak var countDownLabel: UILabel!
72
+
73
+
74
+
75
+
76
+
77
+ @IBAction func settingButtonAction(_ sender: Any) {
78
+
79
+ // timerをアンラップしてnowTimerに代入
80
+
81
+ if let nowTimer = timer {
82
+
83
+ // もしタイマーが、実行中だったら停止
84
+
85
+ if nowTimer.isValid == true {
86
+
87
+ // タイマー停止
88
+
89
+ nowTimer.invalidate()
90
+
91
+ }
92
+
93
+ }
94
+
95
+ // 画面遷移を行う
96
+
97
+ performSegue(withIdentifier: "goSetting", sender: nil)
98
+
99
+ }
100
+
101
+
102
+
103
+ @IBAction func startButtonAction(_ sender: Any) {
104
+
105
+ // timerをアンラップしてnowTimerに代入
106
+
107
+ if let nowTimer = timer {
108
+
109
+ // もしタイマーが、実行中だったらスタートしない
110
+
111
+ if nowTimer.isValid == true {
112
+
113
+ // 何も処理しない
114
+
115
+ return
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ // タイマーをスタート
124
+
125
+ timer = Timer.scheduledTimer(timeInterval: 1.0,
126
+
127
+ target: self,
128
+
33
- ②また、細かい点で恐縮ですが、scheduledTime()メソッドの`selector: #selector(self.timerInterrupt(_:)),//ここです`の指定についてですが、
129
+ selector: #selector(self.timerInterrupt(_:)),//ここです
130
+
34
-
131
+ userInfo: nil,
132
+
133
+ repeats: true)
134
+
135
+
136
+
137
+ }
138
+
139
+
140
+
141
+ @IBAction func stopButtonAction(_ sender: Any) {
142
+
143
+ // timerをアンラップしてnowTimerに代入
144
+
145
+ if let nowTimer = timer {
146
+
147
+ // もしタイマーが、実行中だったら停止
148
+
149
+ if nowTimer.isValid == true {
150
+
151
+ // タイマー停止
152
+
153
+ nowTimer.invalidate()
154
+
155
+ }
156
+
157
+ }
158
+
159
+ }
160
+
161
+
162
+
163
+ func displayUpdate() -> Int {
164
+
165
+
166
+
167
+ // UserDefaultsのインスタンスを生成
168
+
169
+ let settings = UserDefaults.standard
170
+
171
+ // 取得した秒数をtimerValueに渡す
172
+
173
+ let timerValue = settings.integer(forKey: settingKey)
174
+
175
+ // 残り時間(remainCount)を生成
176
+
177
+ let remainCount = timerValue - count
178
+
179
+ // remainCount(残りの時間)をラベルに表示
180
+
35
- documentationを確認すると以下の様に各プロパティに設定される要素が記載されているのですが、aSelectorの"a"とは特に言及がないのですが、何者なのでしょうか?また実際のコードでは`#selector()`の様に見慣れない#をとしてメソッドを設定しているのですが何もdocumentationでは触れられておらず、このコードの意味するところについて、ご教示願えますでしょうか?
181
+ countDownLabel.text = "残り(remainCount)秒"
182
+
36
-
183
+ // 残り時間を戻り値に設定
37
-
38
-
184
+
39
- ```Documentation
185
+ return remainCount
186
+
187
+
188
+
40
-
189
+ }
190
+
191
+
192
+
193
+ // 経過時間の処理
194
+
195
+ @objc func timerInterrupt(_ timer:Timer) {
196
+
197
+
198
+
199
+ // count(経過時間)に+1していく
200
+
201
+ count += 1
202
+
203
+ // remainCount(残り時間)が0以下のとき、タイマーを止める
204
+
205
+ if displayUpdate() <= 0 {
206
+
207
+ // 初期化処理
208
+
209
+ count = 0
210
+
211
+ // タイマー停止
212
+
213
+ timer.invalidate()
214
+
215
+ }
216
+
217
+ }
218
+
219
+
220
+
41
- class func scheduledTimer(timeInterval ti: TimeInterval, target aTarget: Any, selector aSelector: Selector, userInfo: Any?, repeats yesOrNo: Bool) -> Timer
221
+ override func viewDidAppear(_ animated: Bool) {
222
+
223
+ // カウント(経過時間)をゼロにする
224
+
225
+ count = 0
226
+
227
+ // タイマーの表示を更新する
228
+
229
+ _ = displayUpdate()
230
+
231
+ }
232
+
233
+
234
+
235
+
236
+
237
+ }
238
+
239
+
240
+
241
+
42
242
 
43
243
  ```
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
- ```Swift
52
-
53
-
54
-
55
- import UIKit
56
-
57
-
58
-
59
- class ViewController: UIViewController {
60
-
61
-
62
-
63
- var timer : Timer?
64
-
65
- var count = 0
66
-
67
- let settingKey = "timer_value" //ここです
68
-
69
-
70
-
71
- override func viewDidLoad() {
72
-
73
- super.viewDidLoad()
74
-
75
- // Do any additional setup after loading the view.
76
-
77
- let settings = UserDefaults.standard
78
-
79
- settings.register(defaults: [settingKey:10])
80
-
81
- }
82
-
83
-
84
-
85
- @IBOutlet weak var countDownLabel: UILabel!
86
-
87
-
88
-
89
-
90
-
91
- @IBAction func settingButtonAction(_ sender: Any) {
92
-
93
- // timerをアンラップしてnowTimerに代入
94
-
95
- if let nowTimer = timer {
96
-
97
- // もしタイマーが、実行中だったら停止
98
-
99
- if nowTimer.isValid == true {
100
-
101
- // タイマー停止
102
-
103
- nowTimer.invalidate()
104
-
105
- }
106
-
107
- }
108
-
109
- // 画面遷移を行う
110
-
111
- performSegue(withIdentifier: "goSetting", sender: nil)
112
-
113
- }
114
-
115
-
116
-
117
- @IBAction func startButtonAction(_ sender: Any) {
118
-
119
- // timerをアンラップしてnowTimerに代入
120
-
121
- if let nowTimer = timer {
122
-
123
- // もしタイマーが、実行中だったらスタートしない
124
-
125
- if nowTimer.isValid == true {
126
-
127
- // 何も処理しない
128
-
129
- return
130
-
131
- }
132
-
133
- }
134
-
135
-
136
-
137
- // タイマーをスタート
138
-
139
- timer = Timer.scheduledTimer(timeInterval: 1.0,
140
-
141
- target: self,
142
-
143
- selector: #selector(self.timerInterrupt(_:)),//ここです
144
-
145
- userInfo: nil,
146
-
147
- repeats: true)
148
-
149
-
150
-
151
- }
152
-
153
-
154
-
155
- @IBAction func stopButtonAction(_ sender: Any) {
156
-
157
- // timerをアンラップしてnowTimerに代入
158
-
159
- if let nowTimer = timer {
160
-
161
- // もしタイマーが、実行中だったら停止
162
-
163
- if nowTimer.isValid == true {
164
-
165
- // タイマー停止
166
-
167
- nowTimer.invalidate()
168
-
169
- }
170
-
171
- }
172
-
173
- }
174
-
175
-
176
-
177
- func displayUpdate() -> Int {
178
-
179
-
180
-
181
- // UserDefaultsのインスタンスを生成
182
-
183
- let settings = UserDefaults.standard
184
-
185
- // 取得した秒数をtimerValueに渡す
186
-
187
- let timerValue = settings.integer(forKey: settingKey)
188
-
189
- // 残り時間(remainCount)を生成
190
-
191
- let remainCount = timerValue - count
192
-
193
- // remainCount(残りの時間)をラベルに表示
194
-
195
- countDownLabel.text = "残り(remainCount)秒"
196
-
197
- // 残り時間を戻り値に設定
198
-
199
- return remainCount
200
-
201
-
202
-
203
- }
204
-
205
-
206
-
207
- // 経過時間の処理
208
-
209
- @objc func timerInterrupt(_ timer:Timer) {
210
-
211
-
212
-
213
- // count(経過時間)に+1していく
214
-
215
- count += 1
216
-
217
- // remainCount(残り時間)が0以下のとき、タイマーを止める
218
-
219
- if displayUpdate() <= 0 {
220
-
221
- // 初期化処理
222
-
223
- count = 0
224
-
225
- // タイマー停止
226
-
227
- timer.invalidate()
228
-
229
- }
230
-
231
- }
232
-
233
-
234
-
235
- override func viewDidAppear(_ animated: Bool) {
236
-
237
- // カウント(経過時間)をゼロにする
238
-
239
- count = 0
240
-
241
- // タイマーの表示を更新する
242
-
243
- _ = displayUpdate()
244
-
245
- }
246
-
247
-
248
-
249
-
250
-
251
- }
252
-
253
-
254
-
255
-
256
-
257
- ```