ios - swift 错误 : value of optional type 'Double?' not unwrapped

标签 ios iphone xcode swift

我是 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/

相关文章:

ios - 尝试做一个简单的倒计时应用程序但计数器没有倒计时?

ios - 临时应用程序在启动时挂起, "has active assertions but is being debugged"

ios - Xcode 10.2 无法在 iOS < 10 的模拟器上运行应用程序

ios - 我们如何在移动设备(iOS 和 Android)上播放 Scorm 内容?

ios - Visual Studio 2015/Xamarin,调试不工作 : an error occurred while executing MTouch

iphone - iOS HTTP 多部分形式流请求

iphone - 如何在 Cocoa 中进行全文搜索?

ios - iTunes软件服务认证错误域错误434

ios - 如何避免 IAP 30% 的订阅费用(IOS 应用程序)?

ios - Realm 数据库迁移,添加新对象并修改旧对象