DEV Community

Cover image for Day 1 Learning Swift
Jeremy
Jeremy

Posted on • Updated on

Day 1 Learning Swift

I am at the beginning of this journey... "Hello World"

1) Variables

Try reading var as “create a new variable”. So, the first line above might be read out loud as “create a new variable called favoriteShow and give it the value Orange is the New Black.”

Lines 2 and 3 don’t have var in there, so they modify the existing value rather than creating a new variable.

Example
var favoriteShow = "Orange is the New Black"
favoriteShow = "The Good Place"
favoriteShow = "Doctor Who"

2) Strings and integers

Understanding variables... str means string (aka letters) while Int means integer (aka numbers)

Whole numbers like 38 is assigned by Swift the type Int – short for “integer”.

Example
var str = "Hello, World"
var age = 38

If you have large numbers, Swift lets you use underscores as thousands separators – they don’t change the number, but they do make it easier to read.

Example
var population = 8_000_000

Strings and integers are different types, and they can’t be mixed. So, while it’s safe to change str to “Goodbye”, I can’t make it 38 because that’s an Int not a String.

Please note: Also even though "40" has a number inside the quotes, this still creates a string.

Example
var age = "40"

3) Multi-line strings

Swift is very particular about how you write those quote marks: the opening and closing triple must be on their own line, but opening and closing line breaks won’t be included in your final string.

If you only want multi-line strings to format your code neatly, and you don’t want those line breaks to actually be in your string, end each line with a \, like this:

Example
var str2 = """
This goes \
over multiple \
lines
"""

Note: The final three quotes must be on a line by themselves.

The code will read like a perfect sentence.
"This goes over multiple lines"

4) Doubles and Booleans

Two other basic types of data in Swift are doubles and booleans, and you’ll be using them a lot.

“Double” is short for “double-precision floating-point number”, and it’s a fancy way of saying it holds fractional values such as 38.1, or 3.141592654.

Example
var pi = 3.141

var myInt = 1
var myDouble = 1.0

Doubles are different from integers, and you can’t mix them by accident.

As for booleans, they are much simpler: they just hold either true or false, and Swift will automatically assign the boolean type to any variable assigned either true or false as its value.

Example
var awesome = true

Note: Also remember the quotes rule

var sorted = "false"
Although this uses false, it's inside double quotes so this creates a string.

var isVisible = false
This creates a Boolean with the name isVisible.

5) String interpolation

You can place any type of variable inside your string – all you have to do is write a backslash, \, followed by your variable name in parentheses.

Example
var score = 85
var str = "Your score was (score)"

As you can see in the playground output, that sets the str variable to be “Your score was 85”.

You can do this as many times as you need, making strings out of strings if you want:

Example
var score = 85
var str = "Your score was (score)"
var results = "The test results are here: (str)"

String interpolation is written as (reason).

Example
var name = "(firstName) (lastName)"

This uses string interpolation to create a string variable called name.

https://www.hackingwithswift.com/articles/178/super-powered-string-interpolation-in-swift-5-0

6) Constants

Variables are a great way to store temporary data in your programs, but Swift gives us a second option that’s even better: constants. They are identical to variables in every way, with one important difference: we can’t change their values once they are set.

AGAIN...The let keyword creates constants, which are values that can be set once and never again.

Example
let taylor = "swift"

If you try to change that, Xcode will refuse to run your code. It’s a form of safety: when you use constants you can no longer change something by accident.

7) Type annotations

If you want you can be explicit about the type of your data rather than relying on Swift’s type inference, like this:

Example
let album: String = "Reputation"
let year: Int = 1989
let height: Double = 1.78
let taylorRocks: Bool = true

Notice that booleans have the short type name Bool, in the same way that integers have the short type name Int.

Question: When should I use type annotations in Swift?

The answer to the question is primarily one of three reasons:

  1. Swift can’t figure out what type should be used.
  2. You want Swift to use a different type from its default.
  3. You don’t want to assign a value just yet.

The first of those will usually happen only in more advanced code. For example, if you were loading some data from the internet that you know happens to be the name of your local politician, Swift can’t know that ahead of time so you’ll need to tell it.

The second scenario is quite common as you learn more in Swift, but right now a simple example is trying to create a double variable without having to constantly write “.0” everywhere:

Example
var percentage: Double = 99

That makes percentage a double with the value of 99.0. Yes, we have assigned an integer to it, but our type annotation makes it clear that the actual data type we want is double.

The third option happens when you want to tell Swift that a variable is going to exist, but you don’t want to set its value just yet. This happens in lots of places in Swift, and looks like this:

Example
var name: String

You can then assign a string to name later on, but you can’t assign a different type because Swift knows it would be invalid.

8) Simple types: Summary

Let’s summarize.

  1. You make variables using var and constants using let. It’s preferable to use constants as often as possible.

  2. Strings start and end with double quotes, but if you want them to run across multiple lines you should use three sets of double quotes.

  3. Integers hold whole numbers, doubles hold fractional numbers, and booleans hold true or false.

  4. String interpolation allows you to create strings from other variables and constants, placing their values inside your string.

  5. Swift uses type inference to assign each variable or constant a type, but you can provide explicit types if you want.

https://www.hackingwithswift.com/100/swiftui/1

Top comments (0)