質問編集履歴
1
いただいた追記依頼から更新しています
test
CHANGED
File without changes
|
test
CHANGED
@@ -279,3 +279,191 @@
|
|
279
279
|
|
280
280
|
|
281
281
|
よろしくお願いします。
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
---------------------------
|
286
|
+
|
287
|
+
その後、hoshiさんの③のコメントにあるようにstructに使えないということで、質問の上にあったStopwatch.swiftを
|
288
|
+
|
289
|
+
```
|
290
|
+
|
291
|
+
import SwiftUI
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
class Stopwatch2: ObservableObject {
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
/// String to show in UI
|
300
|
+
|
301
|
+
@Published private(set) var message = "Not running"
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
/// Is the timer running?
|
306
|
+
|
307
|
+
@Published private(set) var isRunning = false
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
/// Time that we're counting from
|
312
|
+
|
313
|
+
private var startTime: Date? { didSet { saveStartTime() } }//<--④
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
/// The timer
|
318
|
+
|
319
|
+
private var timer: AnyCancellable?// <--⑤
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
init() {
|
324
|
+
|
325
|
+
startTime = fetchStartTime() //<--⑥
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
if startTime != nil {
|
330
|
+
|
331
|
+
start() //<--⑦
|
332
|
+
|
333
|
+
}
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
// MARK: - Public Interface
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
extension Stopwatch {
|
346
|
+
|
347
|
+
func start() {
|
348
|
+
|
349
|
+
timer?.cancel() // cancel timer if any. <--⑧
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
if startTime == nil { //<--⑨ 以下同様のエラー多数
|
354
|
+
|
355
|
+
startTime = Date()
|
356
|
+
|
357
|
+
}
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
message = ""
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
timer = Timer
|
366
|
+
|
367
|
+
.publish(every: 0.1, on: .main, in: .common)
|
368
|
+
|
369
|
+
.autoconnect()
|
370
|
+
|
371
|
+
.sink { [weak self] _ in
|
372
|
+
|
373
|
+
guard
|
374
|
+
|
375
|
+
let self = self,
|
376
|
+
|
377
|
+
let startTime = self.startTime
|
378
|
+
|
379
|
+
else { return }
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
let now = Date()
|
384
|
+
|
385
|
+
let elapsed = now.timeIntervalSince(startTime)
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
guard elapsed < 60 else {
|
390
|
+
|
391
|
+
self.stop()
|
392
|
+
|
393
|
+
return
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
self.message = String(format: "%0.1f", elapsed)
|
400
|
+
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
isRunning = true
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
func stop() {
|
412
|
+
|
413
|
+
timer?.cancel()
|
414
|
+
|
415
|
+
timer = nil
|
416
|
+
|
417
|
+
startTime = nil
|
418
|
+
|
419
|
+
isRunning = false
|
420
|
+
|
421
|
+
message = "Not running"
|
422
|
+
|
423
|
+
}
|
424
|
+
|
425
|
+
}
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
// MARK: - Private implementation
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
private extension Stopwatch {
|
434
|
+
|
435
|
+
func saveStartTime() {
|
436
|
+
|
437
|
+
if let startTime = startTime {
|
438
|
+
|
439
|
+
UserDefaults.standard.set(startTime, forKey: "startTime")
|
440
|
+
|
441
|
+
} else {
|
442
|
+
|
443
|
+
UserDefaults.standard.removeObject(forKey: "startTime")
|
444
|
+
|
445
|
+
}
|
446
|
+
|
447
|
+
}
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
func fetchStartTime() -> Date? {
|
452
|
+
|
453
|
+
UserDefaults.standard.object(forKey: "startTime") as? Date
|
454
|
+
|
455
|
+
}
|
456
|
+
|
457
|
+
}
|
458
|
+
|
459
|
+
```
|
460
|
+
|
461
|
+
のようにし、③のエラーは消えたのですが、その代わりclass Stopwatch下の各変数がCannot find '????' in scopeになってしまいます。
|
462
|
+
|
463
|
+
例えば④であれば、Cannot find 'saveStartTime' in scopeclass ⑤であればCannot find 'AnyCancellable' in scopeclass です。
|
464
|
+
|
465
|
+
class Stopwatch2: ObservableObject {以下を同一スコープの中に入れる方法がわかりません。
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
質問の仕方が稚拙とは思いますがよろしくお願いします。
|