前提・実現したいこと
時間の差分の取得をしたいです。
発生している問題・エラーメッセージ
バックグラウンドに入っていた間の時間を求めたいのですがうまくできません。
該当のソースコード
Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 6 var bd: Date! 7 var fd: Date! 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 // Do any additional setup after loading the view. 12 13 // 登録 14 NotificationCenter.default.addObserver(self, selector: #selector(viewWillEnterForeground( 15 notification:)), name:UIApplication.willEnterForegroundNotification, object: nil) 16 NotificationCenter.default.addObserver(self, selector: #selector(viewDidEnterBackground( 17 notification:)), name:UIApplication.didEnterBackgroundNotification, object: nil) 18 19 } 20 21 @IBOutlet weak var Label: UILabel! 22 23 @IBOutlet weak var Label1: UILabel! 24 25 @IBOutlet weak var Label2: UILabel! 26 // AppDelegate -> applicationWillEnterForegroundの通知 27 @objc func viewWillEnterForeground(notification: Notification) { 28 let fd = Date()//フォアグラウンドに戻ってきた時間 29 print("(fd)") 30 Label.text = "(fd)" 31 } 32 33 34 35 // AppDelegate -> applicationDidEnterBackgroundの通知 36 @objc func viewDidEnterBackground(notification: Notification) { 37 let bd = Date() 38 print("(bd)") 39 Label1.text = "(bd)" 40 41 42 } 43 44 @objc func didBecomeActive(notifiction: Notification) { 45 46 47 let diff = fd.timeIntervalSince(bd) 48 49 print("(diff)") 50 Label2.text = "(diff)" 51 52 } 53 54} 55 56
試したこと
最後の関数の書き方がわからず、差分が取得できないでいます。いくつか調べつつ試しましたがダメでした。ご教示願います。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/02 13:35