简体   繁体   中英

Finding nil in systemDouble value in Swift 4

I'm trying to find the current system version in Swift 4, I have a label that I will set to either "Updated" or "Not Updated", depending on whether the current iOS version is >= 11.2 or < 11.2.

This works in the emulator just fine, but when I run it on my iPhone X, it returns an error saying: "Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value", stating that systemDouble is equal to nil.

I've tried saying if systemDouble != nil and if systemDouble == nil, but nothing seems to be working.

Any help would be greatly appreciated, thank you

Code below:

@IBOutlet weak var updateStatus: UILabel!


let systemVersion = UIDevice.current.systemVersion
let systemDouble = Double(systemVersion)



if (updateStatus  != nil && systemDouble! >= 11.2)

{
    updateStatus.text! = "Updated"
    updateStatus.textColor = UIColor.green
}

else if (updateStatus != nil && systemDouble! < 11.2)
{
    updateStatus.text! = "Not Updated" + String (systemVersion)
    updateStatus.textColor = UIColor.red

}

systemVersion is a string, and there's no guarantee that it can be converted to a number. So when you force unwrap it, you get an error.

I would suggest using if #available(iOS 11.2, *) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM