0

現在、単純なカウントダウンタイマーアプリを作っております。
カウントダウンの途中で「stop」で一時停止し,「start」で再スタートしたいのですがCannot convert value of type 'Data?' to expected argument type 'Date'と言うエラーの意味がわからずつまずいてます。このエラーを治すにはどのような処置を行えばよろしいでしょうか。

画面をコントロールするクラス↓↓

import UIKit

func secondsToTimerLabel(_ seconds : Int) -> String {
    let min = seconds / 60
    let sec = seconds % 60
    return String(format: "%02d : %02d", min, sec)
}

class NextViewController: UIViewController {

    @IBOutlet weak var countdwonTimer: UILabel!
    @IBOutlet weak var executionTimer: UILabel!
    @IBOutlet weak var stopButton: UIButton!
    @IBOutlet weak var kyannseruButton: UIButton!
    @IBOutlet weak var twentyButton: UIButton!
    @IBOutlet weak var thirtyButton: UIButton!
    @IBOutlet weak var sixtyButton: UIButton!
    @IBOutlet weak var ninetyButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

        stopButton.layer.cornerRadius = 30.0
        kyannseruButton.layer.cornerRadius = 30.0
        twentyButton.layer.cornerRadius = 22.5
        thirtyButton.layer.cornerRadius = 22.5
        sixtyButton.layer.cornerRadius = 22.5
        ninetyButton.layer.cornerRadius = 22.5

        self.executionTimer.text = secondsToTimerLabel(TimerManager.shared.count)

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        TimerManager.shared.onCountDidSet = { count in
            self.executionTimer.text = secondsToTimerLabel(Int(count))
        }

    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)

        TimerManager.shared.onCountDidSet = nil

    }




    @IBAction func stopButtonAction(_ sender: UIButton) {

       if TimerManager.shared.isRunning {

        if TimerManager.shared.timer?.isValid == true{

            TimerManager.shared.stop()
            stopButton.setTitle("start", for: .normal)
            twentyButton.isUserInteractionEnabled = true
            thirtyButton.isUserInteractionEnabled = true
            sixtyButton.isUserInteractionEnabled = true
            ninetyButton.isUserInteractionEnabled = true
            print("111111111111111")


            } else {
        TimerManager.shared.restartCount()
        stopButton.setTitle("stop", for: .normal)
        twentyButton.isUserInteractionEnabled = false
        thirtyButton.isUserInteractionEnabled = false
        sixtyButton.isUserInteractionEnabled = false
        ninetyButton.isUserInteractionEnabled = false
        print("222222222222222222")
          }
        } else {

            TimerManager.shared.startCount()
            stopButton.setTitle("stop", for: .normal)
            twentyButton.isUserInteractionEnabled = false
            thirtyButton.isUserInteractionEnabled = false
            sixtyButton.isUserInteractionEnabled = false
            ninetyButton.isUserInteractionEnabled = false
            print("3333333333333333333")

        }



   }

    @IBAction func cancelButtonAction(_ sender: UIButton) {

        TimerManager.shared.cancel()

        twentyButton.isUserInteractionEnabled = true
        thirtyButton.isUserInteractionEnabled = true
        sixtyButton.isUserInteractionEnabled = true
        ninetyButton.isUserInteractionEnabled = true

        TimerManager.shared.count = 0
        self.executionTimer.text = secondsToTimerLabel(0)

        stopButton.setTitle("start", for: .normal)

    }

    @IBAction func twentyButtonAction(_ sender: UIButton) {
        self.executionTimer.text = secondsToTimerLabel(1200)
        TimerManager.shared.nowNumber = 20
    }

    @IBAction func thertyButtonActiojn(_ sender: UIButton) {
        self.executionTimer.text = secondsToTimerLabel(1800)
        TimerManager.shared.nowNumber = 30
    }

    @IBAction func sixtyButtonAction(_ sender: UIButton) {
        self.executionTimer.text = secondsToTimerLabel(3600)
        TimerManager.shared.nowNumber = 60
    }

    @IBAction func ninetyButtonAction(_ sender: UIButton) {
        self.executionTimer.text = secondsToTimerLabel(5400)
        TimerManager.shared.nowNumber = 90
    }




}

問題のクラスです↓↓、タイマーを司るクラスです↓↓

import UIKit


class TimerManager {
    static var shared = TimerManager()

    let settingKey = "timer_value"
    let settingKeytwenty = "timer_twenty"
    let settingKeythirty = "timer_thirty"
    let settingKeysixty  = "timer_sixty"
    let settingKeyninety = "timer_ninety"

    var timer : Timer?
    var startDate : Date?
    var restartDate : Data?

    var count = 0 {
        didSet {
        onCountDidSet?(Double(count))
        }
    }
    var onCountDidSet : ((_ count : Double) -> Void)? = nil

    var nowNumber : Int = 0

    var restartBool : Bool = false

    init () {
        let settings = UserDefaults.standard
        settings.register(defaults: [settingKey : 216000])
        let settings20 = UserDefaults.standard
        settings20.register(defaults: [settingKeytwenty : 1200])
        let settings30 = UserDefaults.standard
        settings30.register(defaults: [settingKeythirty : 1800])
        let settings60 = UserDefaults.standard
        settings60.register(defaults: [settingKeysixty : 3600])
        let settings90 = UserDefaults.standard
        settings90.register(defaults: [settingKeyninety : 5400])

    }

    func restartCount() {
        restartDate = Data()
        switch nowNumber {

        case 20 :

            timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in self.twntytimer(timer)
            })

        case 30 :
            timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: {timer in self.thritytimer(timer)})


        case 60 :
            timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
                self.sixtytimer(timer)})

        case 90 :
            timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
                self.ninetytimer(timer)})

        default :
            print("Invalid value \(nowNumber)")
        }
    }

    func startCount() {
        startDate = Date()

        if timer == nil {

            switch nowNumber {

            case 20 :
                timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in self.twntytimer(timer)
                })

            case 30 :
                timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: {timer in self.thritytimer(timer)})


            case 60 :
                timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
                    self.sixtytimer(timer)})

            case 90 :
                timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
                    self.ninetytimer(timer)})

            default :
                print("Invalid value \(nowNumber)")
            }
        }
    }

    func stop() {
        timer?.invalidate()
    }


    func cancel() {
        timer?.invalidate()
        timer = nil
    }


    var isRunning : Bool {
        return timer != nil
    }




    func twntytimer(_ timer : Timer) {

        let now = Date()

        let sitting = UserDefaults.standard
        let timeValue = sitting.integer(forKey: settingKeytwenty)

        if restartBool != true {

            guard let startDate = startDate else {
                return
            }
        let dateDifference = now.timeIntervalSince(startDate)
           count = timeValue - Int(dateDifference)

        } else {

            guard let restartDates = restartDate else {
                return
            }
            let dateDifference = now.timeIntervalSince(restartDate)
            count = timeValue - Int(dateDifference) //問題のエラーはここです!!エラー[Cannot convert value of type 'Data?' to expected argument type 'Date']

        }
        if count <= 0 {
            self.timer?.invalidate()
            count = 0
            self.timer = nil

        }

     }

    func thritytimer (_ timer : Timer) {  //現在途中、上のtwntytimerが成功したら同じような仕様にするつもりです

        guard let startDate = startDate else {
            return
        }
        let now = Date()
        let dateDifference = now.timeIntervalSince(startDate)

        let sitting = UserDefaults.standard
        let timeValue = sitting.integer(forKey: settingKeythirty)

        count = timeValue - Int(dateDifference)

        if count <= 0 {
            self.timer?.invalidate()
            count = 0
            self.timer = nil
        }

    }

    func sixtytimer(_ timer : Timer) { //現在途中、上のtwntytimerが成功したら同じような仕様にするつもりです

        guard let startDate = startDate else {
            return
        }
        let now = Date()
        let dateDifference = now.timeIntervalSince(startDate)

        let sitting = UserDefaults.standard
        let timeValue = sitting.integer(forKey: settingKeysixty)

        count = timeValue - Int(dateDifference)

        if count <= 0 {
            self.timer?.invalidate()
            count = 0
            self.timer = nil
        }

    }

    func ninetytimer(_ timer : Timer) { //現在途中、上のtwntytimerが成功したら同じような仕様にするつもりです

        guard let startDate = startDate else {
            return
        }
        let now = Date()
        let dateDifference = now.timeIntervalSince(startDate)

        let sitting = UserDefaults.standard
        let timeValue = sitting.integer(forKey: settingKeyninety)

        count = timeValue - Int(dateDifference)

        if count <= 0 {
            self.timer?.invalidate()
            count = 0
            self.timer = nil
        }

    }


}

この様なアプリです↓
画像の説明をここに入力
画像の説明をここに入力

1 件の回答 1

2
...
    var timer : Timer?
    var startDate : Date?
    var restartDate : Data?
...

restartDateの型アノテーションがData(Dat"a")になっているからですね。

Date型の引数をとるメソッドにData型の変数を与えているので、型が合わないというエラーメッセージです。

そこを直すと今度は

...
    func restartCount() {
        restartDate = Data()
...

の部分が同様のエラーになると思いますので、右辺のData()(Dat"a"())をDateのイニシャライザに直しましょう。

1
  • ありがとうございます!! まさかその様なミスとは、、、 Commented 2019年6月18日 15:54

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。