从Apple Watch发送时间到手机没有返回正确的号码
问题描述:
当我从Apple Watch上的定时器发送数据时,它会一直发送1,而不是发送"message": timerCount
中的实际数字。我怎样才能确保正确的号码被发回到手机,而不是1.我不知道它从哪里拉1。当我打印timerCount
时,它将返回与定时器上相同的编号。从Apple Watch发送时间到手机没有返回正确的号码
let myInterval: TimeInterval = 0.0
var timerCount: Int = 0
var timer = Timer()
func stopPlay() {
let startTime = Date.timeIntervalSinceReferenceDate
print(timerCount) //this prints the actual time
let data: Dictionary<String, AnyObject> = [ "startTime": startTime as AnyObject, "message": timerCount as AnyObject]
if session.isReachable {
session.sendMessage(data, replyHandler: nil, errorHandler: nil)
} else {
let action1 = WKAlertAction(title: "OK", style: .default, handler: stopPlay)
presentAlert(withTitle: "Workout Complete", message: "Connect to your iPhone", preferredStyle: .actionSheet, actions: [action1])
}
}
@IBAction func start_button() {
if isStarted == false {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)
self.startStopButton.setTitle("Stop")
isStarted = true
} else if isStarted == true {
self.timer.invalidate()
self.startStopButton.setTitle("Resume")
isStarted = false
}
}
func update() {
internalDuration -= 1
totalTimeCounter += 1
timerCount += 1
self.elapsedLabel.setText(timeString(time: totalTimeCounter))
}
答
这个变量可以被重置别的地方在你的代码。如果没有在其他地方定义它,那么每次调用更新方法时,它看起来像在update函数中实例化它。
我有'var totalTimeCounter:TimeInterval = 0.0'在我的代码顶部。我剪掉了一堆。在'timerCount'进入字典之前,我正确地输出了正确的数字。 'print(“>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”,timerCount) ' 'let data:Dictionary = [“startTime”:startTime as AnyObject,“message”:timerCount as AnyObject]' –