DEV Community

Cover image for Variables | Constants | Types in Swift
Marcos
Marcos

Posted on

Variables | Constants | Types in Swift

Variables | Constants | Types in Swift are created using var for variables and let for constants.

let number = 3 // Creates a constant with an integer value

var decimal = 3.0 // Creates a variable with a Double value
Enter fullscreen mode Exit fullscreen mode

Swift has basic types such as Int, Float, Double, String, and Bool, and it also allows you to create custom types, but this is more advanced content.

var integer = 3 // Type Int

var float = 0.11111 // Type Float, with up to 6 decimal places of precision

var double = 0.11111111111111 // Type Double, with up to 15 decimal places of precision

var string = "Marcos" // Type String, a single-line string

var multiLineString = """
This is
a multi-line
string
""" // Type String, to create a multi-line string, we use triple quotes ("""") at the beginning and end

var boolean = true // Type Boolean (Bool), which holds a true or false value
Enter fullscreen mode Exit fullscreen mode

As you may notice, in Swift, most of the time we don't need to explicitly specify the type. This is due to Type Inference, which automatically assigns the appropriate type to the variable or constant based on the value assigned to it.

Top comments (0)