Swift is a powerful and intuitive programming language for macOS, iOS, watchOS, tvOS and beyond.
Apple
It surprised me when I saw that no one had done a Swift crash course before, so I decided to make one. BTW I wrote this in like an hour so tell me if there are any typos.
Background
Swift was based on many different languages. Some of these include Python, Java, Rust, and more. Similar to Rust, Swift was made to have high usability and performance. Swift uses a compiler to run its code (similar to C/C++). Anyway, enough background, time to get started.
Getting Started
As tradition suggests, the first line of code should print Hello, World. In Swift, you accomplish this using similar syntax with Python 3.x print("Hello, World!") Although it is similar in syntax to python, you cannot use single quotes.
Declaring variables
Similar to C/C++ and some other languages, you have to declare variables before using them. This is done by using the var keyword. e.g. var myVariable = 5 You can also specify the type of a variable using a colon. The types of variables are
Int - an integer such as 5 or -23
Float - a number with a decimal point such as 4.1 or 1.0 or -5.3
Double - a more accurate float
Bool - true or false
String - a collection of characters such as "Hello, People"
Character - a single letter or character such as "A" Note: Swift is very sensitive when it comes to capitals vs lowercase
Here's an example of declaring variable type var myVariable: Int = 6
Declaring variables used later
This section will be short. To declare variables that you want to use later, but don't know the value of it yet, just declare the type. e.g. var myVariable: Int
Another type of storage - Constants
Constants are variables that can't be changed. If you try to change it, the Swift compiler will get mad.
You declare constants using the keyword let e.g. let myVariable = 5 Another note: you do not need to declare variable type- normally the compiler assumes it for you.
Do Maths
You might be asking yourself, but what if I wanted to add variables!
Doing math stuff in Swift is very similar to other languages:
+ - add
- - subtract
* - multiply
/ - divide
% - get remainder
e.g.
var x = 5
var y = 2
x = y + 4
Compound operators
Similar to other languages, if you wanted to add a number to a variable and make it equal to that same variable, you can shorten it using
+=
-=
*=
or /=
e.g.
var myVar = 5
myVar = myVar + 3
can be shortened using
var myVar = 5
myVar += 3
Printing variables
Let's say you were writing a code where it takes two numbers and then adds them. But how do you print them!
It's quite simple actually. Here's how you do it: print("Here is my variable! \(variableName)") If you couldn't tell, you escape the variable using \(variableName)
You could also print the variable by itself. e.g.
var myVar = 5
print(myVar)
Comments
Single line comments use // e.g. // If this was in code, it wouldn't run! Multi-line comments start with /* and end with */ eg.
/*
none of this will run
ashdgfjhasdg
sadfhgsdajhfgkashjdgfkahjs
*/
If and Else statements
Let's say you wanted to print something based off if a variable is 3 or not. You would you an if/else statement which looks like:
if condition {
// code to be run
} else {
// code to be run if condition is false
}
**Remember to close and open with brackets.
Else If statements
You can also combine multiple if/else statements (similar to elif/eslif). The syntax is:
if condition1 {
// code
} else if condition2 {
// more code
} else {
// even more code
}
Comparison Operators
Comparison Operators are things that evaluate in if/else statements. They include
< // less than
> // greater than
<= // less than or equal to
>= // greater than or equal to
== // equal to
!= // not equal to
e.g.
var myVar = 5
if myVar == 2 {
print("It's two!")
} else if myVar > 6 {
print("It's greater than 6!")
} else {
print("IDK what it is")
}
Logic Operators
There are 3 main Logic Operators:
and - sees if both statements are true
or - sees if one or both statements are true
not - reverses the value from true to false and vice versa
In Swift, these are achieved by ! for not, && for and, and || for or. e.g.
var t = true
var f = false
print(t || f) // prints true
print(t && f) // prints false
print(!t) // prints false
You can also combine these and use parenthenses for clarification.
Loops
Ranges
Ranges are created using the ... operator. e.g. let myVar = 2...7
For-in loop
A For-In loop iterates over a collection of things. e.g.
for char in "Hello, world!" {
print(char)
}
Continue
Putting continue in a loop will make the loop skip to the next iteration.
Break
Break is similar to continue, except when you use break, the loop stops.
Underscore in For-in loops
If you never use the variable before the in in a for-in loop, you can use an underscore _ to signify it. If you don't, the compiler throws a warning at you.
While loop
Similar to a For-in loop, a while loop is also a loop. Except for a while loop keeps iterating until the provided condition is true e.g.
var myCount = 1
let stop = 5
while counter < stop {
print(counter)
counter += 1
}
Getting User Input
Use readLine()! if you want to get a user input. e.g.
var user = readLine()!
print(user)
Although the ! is not required, if you don't use it, the compiler will throw an error at you.
It surprised me when I saw that no one had done a Swift crash course before, so I decided to make one. BTW I wrote this in like an hour so tell me if there are any typos.
Background
Swift was based on many different languages. Some of these include Python, Java, Rust, and more. Similar to Rust, Swift was made to have high usability and performance. Swift uses a compiler to run its code (similar to C/C++). Anyway, enough background, time to get started.
Getting Started
As tradition suggests, the first line of code should print Hello, World. In Swift, you accomplish this using similar syntax with Python 3.x
print("Hello, World!")
Although it is similar in syntax to python, you cannot use single quotes.
Declaring variables
Similar to C/C++ and some other languages, you have to declare variables before using them. This is done by using the
var
keyword.e.g.
var myVariable = 5
You can also specify the type of a variable using a colon. The types of variables are
Int
- an integer such as 5 or -23Float
- a number with a decimal point such as 4.1 or 1.0 or -5.3Double
- a more accurate floatBool
- true or falseString
- a collection of characters such as "Hello, People"Character
- a single letter or character such as "A"Note: Swift is very sensitive when it comes to capitals vs lowercase
Here's an example of declaring variable type
var myVariable: Int = 6
Declaring variables used later
This section will be short. To declare variables that you want to use later, but don't know the value of it yet, just declare the type.
e.g.
var myVariable: Int
Another type of storage - Constants
Constants are variables that can't be changed. If you try to change it, the Swift compiler will get mad.
You declare constants using the keyword
let
e.g.
let myVariable = 5
Another note: you do not need to declare variable type- normally the compiler assumes it for you.
Do Maths
You might be asking yourself, but what if I wanted to add variables!
Doing math stuff in Swift is very similar to other languages:
+
- add-
- subtract*
- multiply/
- divide%
- get remaindere.g.
Compound operators
Similar to other languages, if you wanted to add a number to a variable and make it equal to that same variable, you can shorten it using
e.g.
can be shortened using
Printing variables
Let's say you were writing a code where it takes two numbers and then adds them. But how do you print them!
It's quite simple actually. Here's how you do it:
print("Here is my variable! \(variableName)")
If you couldn't tell, you escape the variable using
\(variableName)
You could also print the variable by itself.
e.g.
Comments
Single line comments use
//
e.g.
// If this was in code, it wouldn't run!
Multi-line comments start with
/*
and end with*/
eg.
If and Else statements
Let's say you wanted to print something based off if a variable is 3 or not. You would you an if/else statement which looks like:
**Remember to close and open with brackets.
Else If statements
You can also combine multiple if/else statements (similar to elif/eslif).
The syntax is:
Comparison Operators
Comparison Operators are things that evaluate in if/else statements.
They include
e.g.
Logic Operators
There are 3 main Logic Operators:
In Swift, these are achieved by
!
for not,&&
for and, and||
for or.e.g.
You can also combine these and use parenthenses for clarification.
Loops
Ranges
Ranges are created using the
...
operator.e.g.
let myVar = 2...7
For-in loop
A For-In loop iterates over a collection of things.
e.g.
Continue
Putting
continue
in a loop will make the loop skip to the next iteration.Break
Break
is similar tocontinue
, except when you use break, the loop stops.Underscore in For-in loops
If you never use the variable before the
in
in a for-in loop, you can use an underscore_
to signify it. If you don't, the compiler throws a warning at you.While loop
Similar to a For-in loop, a while loop is also a loop. Except for a while loop keeps iterating until the provided condition is true
e.g.
Getting User Input
Use
readLine()!
if you want to get a user input.e.g.
Although the ! is not required, if you don't use it, the compiler will throw an error at you.
That's all I have for now.
Upvote for part 2!
@XanderEhlert awesome