DEV Community

Tammy Vo
Tammy Vo

Posted on • Edited on

100 days of SwiftUI - Day 1 & 2

100 days of SwiftUI - Day 1 - Simple Types

100 days of SwiftUI - Day 2 - Simple Types Part 2

Variables:

To create a new variable we need to use "var" to initialize and define.

var operatingSystem = "macOS"
Enter fullscreen mode Exit fullscreen mode

Strings and Integers:

Swift is type safe.
Strings and Integers can not be mixed.
If there are larger numbers, we can use underscores as thousand separators making it easier to read.

var str = "Hello"
var age = 25
var population = 8_000_000
Enter fullscreen mode Exit fullscreen mode

Multi-line String:
Start and end with three double quote marks for multi-line strings, this will include line breaks.

var str1 = """
This goes
over multiple
lines
"""

// This way will not show new line breaks in output
var str2 = """
This goes \
over multiple \
lines
"""
Enter fullscreen mode Exit fullscreen mode

Doubles and booleans:
Doubles will hold a decimal value:

var pi = 3.141
Enter fullscreen mode Exit fullscreen mode

Booleans will hold a true or false value:

var isCool = true
Enter fullscreen mode Exit fullscreen mode

String interpolation:
Place any type of variable inside your string by adding a backslash \

var score = 85
var str = "Your score was \(score)"
var results = "The test results are here: \(str)"
Enter fullscreen mode Exit fullscreen mode

Constants
Use the let keyword to create constants. Constants can be set once and never changed.

let taylor = "swift"
Enter fullscreen mode Exit fullscreen mode

Type annotations
Swift assigns each variable and constant a type based on what value it’s given when it’s created.

To be explicit about the data type, we can assign it:

let album: String = "Reputation"
let year: Int = 1989
let height: Double = 1.78
let taylorRocks: Bool = true
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vulcanwm profile image
Medea

Woah I don’t know about the underscore in numbers fact!
Nice content!

Collapse
 
gamya_m profile image
Gamya

This is such a clean and well-organized summary of Days 1 and 2! 🌸 The way you've broken down each concept with clear code examples makes it really easy to follow along. I especially liked that you included both versions of the multiline string—showing the difference between the one that preserves line breaks and the one that doesn't is a small detail that a lot of beginners miss but really matters in practice!

String interpolation and type annotations are two of those fundamentals that show up absolutely everywhere as you go deeper into Swift, so having them documented this clearly is going to be a great reference to look back on.