我是 Swift 的新手,这是什么错误:
let lvt=self?.lastVibrationTime
let delta=self!.deltaTime
let sens=self!.shakeSensitivity
let time:Double = CACurrentMediaTime()
//error is on `lvt` and says : Error:(37, 27) value of optional type 'Double?' not unwrapped; did you mean to use '!' or '?'?
if time - lvt > delta && data.userAcceleration.x < sens {
println("firmly shaken!")
self?.vibrateMe()
}
最佳答案
当你写let lvt=self?.lastVibrationTime
当使用self?
你的lvt变量是可选的,你必须在使用它之前解包,你有很多解决方案修复此错误:
1. let lvt = self?.lastVibrationTime ?? 5 // 5 is the default value, you can use the value you want
2. let lvt = self!.lastVibrationTime
3. You can unwrap the value before use it:
if let lvt = self?.lastVibrationTime {
// your code here...
}
关于ios - swift 错误 : value of optional type 'Double?' not unwrapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373958/