Fast intro to Swift

Table of Contents:


This should be a short introduction with hello world Swift examples.

Meet let and var

Swift uses constant and variables to assign literals:

Int literals

let a = 1 // constant
var b = 1 + (2 * a) // variable
print("a and b:", a, "and", b)

Output:

a and b: 1 and 3

Float literals

let π = Float.pi // constant
var π_var = 3*π/2 + 1 // variable
print("floats:", π, π_var)

Output:

floats: 3.1415925 5.712389

String literals

// string literal
let str = "A new day for Swift!" 
// emoji variable as string literal
var 👍 = "ok"
print(str, 👍)
👍 = "👍"
print(str, 👍)

Output:

A new day for Swift! ok
A new day for Swift! 👍

You cannot assign to a constant.

Example that won’t work:

let a = 1 
a = 10

Output:

error: cannot assign to value: 'a' is a 'let' constant
note: change 'let' to 'var' to make it mutable

Literal types

We already meat String literals. But there are also:

  • Int
  • Double
  • Float
  • Boolean

literals.

You can check the type of the literal using type function.

var fce : Float = 3
print(type(of: "👍"), type(of: 1), type(of: 3.0), type(of: fce), type(of: true))

Output:

String Int Double Float Bool

Swift type inference

Example:

var fce : Float = 1 + 2
print(fce)

Output:

3.0

Swift tuples

Tuples exists also in Swift.

Example:

let tuple = ("1", 2, (Float)(1+2))
print(tuple)

Output:

("1", 2, 3.0)

For loop

Custom for loop would involve Swift slicing ... operator. (..< as well)

for x in 1...3 {
    print(x)
}

Output:

1
2
3

Swift functions

In short, use the func keyword to define custom functions.

Example:

import Foundation
func sc(val: Float) -> (Float, Float) {
    return (sin(val), cos(val))
}
print(sc(val: 1))

Output:

(0.84147096, 0.5403023)

Function can have optional parameters

Note the parameter val we have to specify. We may omit the parameter if we use _ like this:

import Foundation
func sc(_ val: Float) -> (Float, Float) {
    return (sin(val), cos(val))
}
print(sc(1))

The output will be equivalent.

We can assign functions to tuples

Example:

let (s, c) = sc(1)

Arrays

To define an empty array you need to set the type.

var array = [] // won't work

would simple output the error:

empty collection literal requires an explicit type

This would be correct way to define an empty integer array:

var array : [Int] = []

Exact same array you can get with the following line:

var array : Array<Int> = []

[Int] is a syntactic sugar for Array<Int>

Let’s print array and it’s type:

print(array, type(of: array))

Output:

[] Array<Int>

Once we have an empty array we can start adding elements using the append method:

array.append(1)

We could start with the non empty array:

Example:

let array = [1,2,3]
print(array)

Output:

[1, 2, 3]

Note how array elements are inside [] brackets.

Array map

There is also a map function you will use with arrays. map applies a function to every element in array.

Example:

var array = [1,2,3]
print(array)
array = array.map({ el in el * 2 })
print(array)

Output:

[1, 2, 3]
[2, 4, 6]

Array Filter

Filter returns the new array where the condition inside the filter evaluates to true. In our case elements that are odd. $0 represents the current element.

Example:

var array = [1,2,3]
print(array)
array = array.filter({ $0 % 2 == 1})
print(array)

Output:

[1, 2, 3]
[2, 3]

tags: intro & category: swift