このコードのビルドをしましたがエラーが出ました。
エラーは、
fatal error: unexpectedly found nil while unwrapping an Optional value
でした。
エラーが出ている位置は、
Swift
1// 基準日時の設定 3分を日付型に変換 2let dt:Date = df.date(from: lbTimer.text!)! 3
全文コードは下記の通りです。
Swift
1@IBAction func countDown(_ sender: UIButton) { 2 3 // 背景色 4 self.view.backgroundColor = UIColor.yellow 5 6 // タイマー生成、開始 1秒後の実行 7 let tmr: Timer! = Timer.scheduledTimer( 8 timeInterval: 1.0, // 時間間隔 9 target: self, // タイマーの実際の処理の場所 10 selector: #selector(TimerViewController.tickTimer(_:)), // メソッド タイマーの実際の処理 11 userInfo: nil, 12 repeats: true) // 繰り返し 13 tmr.fire() 14 } 15 16 // タイマー処理 17 func tickTimer(_ timer: Timer) { 18 19 //NSLog(@"タイマー表示"); 20 21 // 時間書式の設定 22 let df:DateFormatter = DateFormatter() 23 df.dateFormat = "mm:ss" 24 25 // 基準日時の設定 3分を日付型に変換 26 let dt:Date = df.date(from: lbTimer.text!)! 27 28 // カウントダウン 29 let dt02 = Date(timeInterval: -1.0, since: dt) 30 31 self.lbTimer.text = df.string(from: dt02) 32 33 // 終了判定 3分が00:00になったら isEqualToString:文字の比較 34 if self.lbTimer.text == "00:00" { 35 36 // バックアップ背景色の変更 37 self.view.backgroundColor = UIColor.red 38 39 // タイマー停止 40 timer.invalidate() 41 42 // カスタマイズ編:ダイアログを作成 43 let alertController = UIAlertController(title: "終了", message: "タイマー終了時間です", preferredStyle: .alert) 44 // ダイアログに表示させるOKボタンを作成 45 let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil) 46 // アクションを追加 47 alertController.addAction(defaultAction) 48 // ダイアログの表示 49 present(alertController, animated: true, completion: nil) 50 51 } 52 } 53
使用しているSwiftは3です。
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/07 07:38
2017/04/07 07:43