Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to float conversion in Swift Beta 5

Tags:

xcode

ios

swift

The following code doesn't work anymore in Beta 5.

let device = UIDevice.currentDevice()
if (device.systemVersion.bridgeToObjectiveC().floatValue < 8.0) {
    [skipped]
}    

Also, it's not possible to downcast a string to float.

let device = UIDevice.currentDevice()
let version: Float = device.systemVersion as Float

The snippet above returns the 'String' is not convertible to 'Float' error.

Any ideas how should we test the OS version now? Thanks.

like image 743
Michael Samoylov Avatar asked Aug 07 '14 09:08

Michael Samoylov


2 Answers

floatValue is a method of NSString.
Since bridgeToObjectiveC() is no longer available with Xcode 6 Beta 5, you can use the as operator to cast your Swift String to NSString:

(device.systemVersion as NSString).floatValue 
like image 171
Thomas Zoechling Avatar answered Oct 23 '22 22:10

Thomas Zoechling


I am using this approach for now

("1.3" as NSString).floatValue
like image 36
Kirsteins Avatar answered Oct 23 '22 21:10

Kirsteins