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.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more