100 days of Swift – Day 1

100 days of Swift – Day 1 – Simple Types

Variables:

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

var operatingSystem = “macOS”

Strings and Integers:

Swift is type safe.
Strings and Integers can not be mixed.
If …

100 days of Swift – Day 1 – Simple Types

Variables:

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

var operatingSystem = "macOS"

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

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
"""

Doubles and booleans:
Doubles will hold a decimal value:

var pi = 3.141

Booleans will hold a true or false value:

var isCool = true

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)"

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

let taylor = "swift"

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

Print Share Comment Cite Upload Translate
APA
Tammy Vo | Sciencx (2024-03-29T07:42:14+00:00) » 100 days of Swift – Day 1. Retrieved from https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/.
MLA
" » 100 days of Swift – Day 1." Tammy Vo | Sciencx - Saturday July 23, 2022, https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/
HARVARD
Tammy Vo | Sciencx Saturday July 23, 2022 » 100 days of Swift – Day 1., viewed 2024-03-29T07:42:14+00:00,<https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/>
VANCOUVER
Tammy Vo | Sciencx - » 100 days of Swift – Day 1. [Internet]. [Accessed 2024-03-29T07:42:14+00:00]. Available from: https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/
CHICAGO
" » 100 days of Swift – Day 1." Tammy Vo | Sciencx - Accessed 2024-03-29T07:42:14+00:00. https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/
IEEE
" » 100 days of Swift – Day 1." Tammy Vo | Sciencx [Online]. Available: https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/. [Accessed: 2024-03-29T07:42:14+00:00]
rf:citation
» 100 days of Swift – Day 1 | Tammy Vo | Sciencx | https://www.scien.cx/2022/07/23/100-days-of-swift-day-1/ | 2024-03-29T07:42:14+00:00
https://github.com/addpipe/simple-recorderjs-demo