filmov
tv
How to Convert Strings into Double, Float, and Int Numbers Using Swift 2 - Swift Tips 4
Показать описание
How to Convert Strings into Double, Float, and Int Numbers Using Swift 2 - Swift Tips 4
In Swift 2, you get native support for working with different numeric types (Float, Double, and Int) and Strings. Prior to Swift 2, you had to do a lot of extra work to convert between types.
Swift 2 allows you to quickly change a String into a number (if it’s a valid conversion).
To convert, you need to use a new style, which is Apple’s recommended best practice.
Use the initializer for the type instead of the toInt() style that was available in Swift 1.2 and earlier.
Use Int and variants like Int32, UInt64, etc. to convert integer values.
var wholeNumber = Int("27")
Use Float or Double to convert floating-point values (real numbers).
let lessPrecisePI = Float("3.14")
let morePrecisePI = Double("3.1415926536")
These conversions can fail, which is why the Int(), Float(), and Double() initializers return an optional type (i.e.: Double?, Float?, Int?, etc.). If the value cannot be converted, the value will be nil.
let invalidNumber = Float("alphabet") // nil, not a valid number
To use the numbers, you need to unwrap them – I recommend using the if let syntax for user input from a UITextField (or you can try the new guard statement in Swift 2!).
print("The user entered a value price of \(cost)")
} else {
}
You will need to unwrap the optional to get the value. The if let syntax above makes it safe to use cost if it’s a valid number, otherwise you can handle the invalid case (i.e.: “alphabet” is not a valid number, so it would become nil).
The new initializers for different value types have made Swift feel more unified – the simple conversions between types are a lot easier and straightforward. User input and JSON data parsing is much easier in Swift 2.
—
Learn how to make your own iPhone apps using Swift 2, iOS 9, and Xcode 7.
In Swift 2, you get native support for working with different numeric types (Float, Double, and Int) and Strings. Prior to Swift 2, you had to do a lot of extra work to convert between types.
Swift 2 allows you to quickly change a String into a number (if it’s a valid conversion).
To convert, you need to use a new style, which is Apple’s recommended best practice.
Use the initializer for the type instead of the toInt() style that was available in Swift 1.2 and earlier.
Use Int and variants like Int32, UInt64, etc. to convert integer values.
var wholeNumber = Int("27")
Use Float or Double to convert floating-point values (real numbers).
let lessPrecisePI = Float("3.14")
let morePrecisePI = Double("3.1415926536")
These conversions can fail, which is why the Int(), Float(), and Double() initializers return an optional type (i.e.: Double?, Float?, Int?, etc.). If the value cannot be converted, the value will be nil.
let invalidNumber = Float("alphabet") // nil, not a valid number
To use the numbers, you need to unwrap them – I recommend using the if let syntax for user input from a UITextField (or you can try the new guard statement in Swift 2!).
print("The user entered a value price of \(cost)")
} else {
}
You will need to unwrap the optional to get the value. The if let syntax above makes it safe to use cost if it’s a valid number, otherwise you can handle the invalid case (i.e.: “alphabet” is not a valid number, so it would become nil).
The new initializers for different value types have made Swift feel more unified – the simple conversions between types are a lot easier and straightforward. User input and JSON data parsing is much easier in Swift 2.
—
Learn how to make your own iPhone apps using Swift 2, iOS 9, and Xcode 7.