Day 1 is all about variables, constants, strings, and numbers.
The first piece of content is titled Why Swift?
Why swift?
- started in 2014, bypasses language cruft built up over time.
- makes it hard to write unsafe code
- Makes it easy to write code that is clear aun understandalbe
- Supports all the world’s languages out of the box
SwiftUI
- deals with drawing things on screen
- came into the world in 2019
No cruft or confusion, just power at your finger tips.
About this course
The course is set up to help people learn the material in an effective manner. The goal is also to remember what you’ve learned.
It doesn’t strive to teach all of Swift, just the parts that we may find most useful.
Designed to be a hands on experience. Relentlessly focused on trying it yourself.
How to follow along
Type the code out. Run it. See if it works.
Use the playground.
How to create variables and constants
variables are used to store data
- Variables:
A variable’s value can vary and change over time.
import Cocoa var greeting = "Hello playground" // var means create a variable // greeting is the descriptive name of the variable // = does the assignment // "hello playground" is the initial value we are storing in the variable // redefining the variable var name = "Ted" name = "Rebecca" name = "Keeley"
- Constants
A constant can not be changed after it is defined
let character = "Daphne" // can not be changed character = "Bill" // won't work
codingConvention
- Use camel-case
- Prefer constants over variables to prevent run time changes
Scored 6/6 on Variables
Scored 6/6 on Constants
How to create strings
Strings are created like any other variable/constant, but enclosed in double quotes.
Multi-line strings are enclosed with 3 double quotes at the beginning and end. They aren’t used very often.
To escape a string, you would use the back slash character \
before the character you are escaping.
Scored 12/12 on Strings
How to store whole numbers
Whole numbers are integer type. They are created the same way other variables are created minus the double quotes. instead of commas, we can use underscores.
instead of doing this:
let num = 1000000000
we can do
let num = 100_000_000
Whole numbers can also be defined as the result of a calculation:
let score = 10 let lowerScore = score - 2
compound assignment operators
We can increment a integer variable like this:
var counter = 10 counter = counter + 5
or we can do this:
// we use var because the value is changing... var counter = 10 counter += 5 // or counter -= 10 // or counter /= 2 // or counter *= 3
Integer methods
let number = 120 print(number.isMultiple(of: 3)) or print(120.isMultiple(of: 3))
Scored 12/12 on Strings & Integers
How to decimal numbers
Decimal numbers are stored similar to integers. They have a floating point in the value.
Ex.
let num = 5.23
However this results in a double. Ex.
let num = 0.1 + 0.2 // "0.30000000000000004"
You can not mix int values and double values. It must be explicitly expressed.
let a = 1 let b = 2.0 let c = Double(a) + b
There is also the CGFloat type and they can be used alongside the Double type
Once Swift has decided what kind of data you are storing, its type can not be changed.
This works
var name = "a" name = "b"
This does not and will result in an error
var name = "a" name = 34
Type safety is important because as the application grows, it is extremely hard to keep the types of variables in context