In Swift, we need not give explicit types to these variables. They are implicitly inferred from the type of value they were assigned. This is known as Type inference.
Type inference allows us to omit the variable type when we define it. The compiler will infer the type, based on the initial value.
However, we can explicitly define type if needed.
var x :Int= 10
An integer is a whole number. Integers can be either signed (positive, negative, or zero) or unsigned (positive or zero). Swift provides several integer types of different sizes.
Following table shows different integer types and their range of values:
Float and Double
Float represents a 32-bit floating-point number, while Double represents a 64-bit floating-point number.
let num1 : Float = 1.23
let num2 : Double = 3.14
It is recommended to use the Double type over the Float type unless there is a specific reason to use the latter.
Swift has a built-in Boolean type called Bool that accepts one of the two built-in Boolean constants. These constants are true and false.
var boolFalse :Bool = false
var boolTrue : Bool = true
A String type represents a collection of characters.
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post