Swift 错误:可选类型“Double?"的值没有打开 [英] Swift error : value of optional type 'Double?' not unwrapped
本文介绍了Swift 错误:可选类型“Double?"的值没有打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 Swift 新手,这是什么错误:
I am newbie in Swift, what is this error :
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变量是可选的,你必须在使用之前解开它,你有很多解决这个错误的方法:
When you write let lvt=self?.lastVibrationTime
when using self?
your lvt variable is optional, you have to unwrap it before using it, you have many solutions to fix this error:
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...
}
这篇关于Swift 错误:可选类型“Double?"的值没有打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文