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.

Sentry mobile image

App store rankings love fast apps - mobile vitals can help you get there

Slow startup times, UI hangs, and frozen frames frustrate users—but they’re also fixable. Mobile Vitals help you measure and understand these performance issues so you can optimize your app’s speed and responsiveness. Learn how to use them to reduce friction and improve user experience.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay