Today’s Goal:
To help you fake your way through a job interview
Slide 3
Slide 3 text
Agenda
1. The history of Swift
2. How Swift became open source
3. Swift syntax
Slide 4
Slide 4 text
History
Slide 5
Slide 5 text
First, we need to talk about
Objective-C
Slide 6
Slide 6 text
Objective-C has advanced as far
as it can
Slide 7
Slide 7 text
Replacing Objective-C was a
strategic necessity
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
Requirements for an Objective-C replacement
1. No pointers
2. Managed memory
3. Native unicode support
4. Concise
5. Named parameters
Slide 10
Slide 10 text
Swift expectations
Slide 11
Slide 11 text
Open Source
Swift
Slide 12
Slide 12 text
Swift Evolution
Slide 13
Slide 13 text
bugs.swift.org
Slide 14
Slide 14 text
Swift on
Linux
Slide 15
Slide 15 text
Swift is a multi-purpose language
Slide 16
Slide 16 text
Swift
Syntax
Slide 17
Slide 17 text
Swift is
multi-platform
and
multi-paradigm
Slide 18
Slide 18 text
Types
A type is a classification of data which tells the
computer how the programmer intends to use that
data.
— Wikipedia
So basically programmers tell the computer their
intentions and the computer will complain if the
programmer fails to adhere to them.
Slide 19
Slide 19 text
Types
Example types:
—Integers
—Booleans
—Functions
—Classes
Swift is strongly-typed.
Slide 20
Slide 20 text
Built-in Types
—String (ex: "Hello, world!")
—Int (ex: 1234 or -1)
—Bool (ex: true or false)
—etc...
Slide 21
Slide 21 text
Enums
An enum is a type with mutually-exclusive cases.
enum ArtworkAvailability {
case notForSale
case forSale
case onHold
case sold
}
ArtworkAvailability.forSale
Slide 22
Slide 22 text
Emoji
Swift has full unicode support, so you can make
enum cases have emoji, if you want to.
enum Animals {
case !
case "
case #
}
Slide 23
Slide 23 text
Enums
Enums are super-powerful. Here’re two examples:
enum Bool {
case true
case false
}
enum Never {
}
Slide 24
Slide 24 text
Arrays
Arrays are variable-length data containers for
homogenous data.
["Orta", "Ash", "Eloy", "Sarah", "Maxim"]
[1, 200, -5, 73]
[ArtworkAvailability.forSale, .onHold, .sold]
Slide 25
Slide 25 text
Variables
Variables are a place to store data and refer to it by
name. Variables in Swift are defined one of two
ways:
—var variables whose values can change.
—let constants whose values can never change.
Swift generally prefers let. If you use var when you
don't need it, the compiler will suggest using let
instead.
Slide 26
Slide 26 text
Type Inference
Swift is a strongly-typed language, but compare
these two equivalent lines of code:
let array: [String] = ["Ash", "Orta", "Sarah"]
...
let array = ["Ash", "Orta", "Sarah"]
In either case, Swift needs to disambiguate what is
stored in the array.
Slide 27
Slide 27 text
Tuples
Tuples are fixed-length data containers for
heterogenous data.
let presenter = ("Ash", [Animal.!, .!])
presenter.0 // "Ash"
presenter.1 // [Animal.!, .!]
presenter.2 // compiler error!
Slide 28
Slide 28 text
Named Tuples
Swift also supports named tuples to access
elements at specific positions in a tuple.
let presenter = (name: "Ash", pets: [Animal.!, .!])
presenter.name // "Ash"
presenter.pets // [Animal.!, .!]
Slide 29
Slide 29 text
Functions
Functions are pieces of code. They can optionally
accept input and/or return output.
func sayHi() {
print("Hello, world!")
}
sayHi()
Classes
Classes contain data and are passed by reference.
class Person {
let name: String
let dateOfBirth: Date
...
}
let person = Person(name: "Ash", dateOfBirth: ...
Slide 34
Slide 34 text
Structs
Structs contain data and are passed by value.
struct Person {
let name: String
let dateOfBirth: Date
...
}
let person = Person(name: "Ash", dateOfBirth: ...
Slide 35
Slide 35 text
Structs vs Classes
—Pass by value vs. by reference.
—Classes can extend other classes for OOP.
—Structs have no hierarchy, cannot extend other
structs.
Slide 36
Slide 36 text
Extensions
Extensions are the ability to add functions to
existing types.
extension Int {
func isNegative() -> Bool {
return self < 0
}
}
1.isNegative() // returns false
(-1).isNegative() // returns true
Protocols
Protocols are for loosely-coupled contracts. In other
languages, they're sometimes called interfaces.
They are a way to know that a type has some
functions without knowing what that type is.
Example: Table view data source.
Slide 40
Slide 40 text
Extending for Protocol Conformance
Extensions can add protocol conformance to
existing types, too.
protocol MyProtocol {
func doSomething()
}
extension Int: MyProtocol {
func doSomething() {
...
}
}
Slide 41
Slide 41 text
Optionals
Optionals are a way to define whether or not a value
can be nil, or "missing". Optionals are signified
by ? and are a type. So Int? is an optional integer
because it might be nil.
This is another improvement over Objective-C.
Slide 42
Slide 42 text
Generics
Generic types are types that hold other data – any
data. For example, Array is a generic because it
can hold any type. Let's make a queue.
Extensions
Extensions add behaviour to types.
extension Int {
var isEven: Bool {
return (self % 2 == 0)
}
}
Slide 45
Slide 45 text
Extensions
protocol Occupiable {
var isEmpty: Bool
var isNotEmpty: Bool
}
extension String: Occupiable {
var isEmpty: Bool {
return self.characters.count > 0
}
var isNotEmpty: Bool {
return !self.isEmpty
}
}
Slide 46
Slide 46 text
Extensions
protocol Occupiable {
var isEmpty: Bool
var isNotEmpty: Bool
}
extension Occupiable {
var isNotEmpty: Bool {
return !self.isEmpty
}
}
extension String: Occupiable {}
Slide 47
Slide 47 text
Extensions with Generics
We can extend generic types with where clauses so
the extension only exists when that clause is true.
For example, if we want to extend arrays of things
that conform to Occupiable:
extension Array where Element: Occupiable {
var isFilledWithEmpties: Bool {
return self.filter({ $0.isNotEmpty }).count > 0
}
}
Wrap-up
1. Swift was a strategic replacement for Objective-C
2. Open sourcing Swift has been good for the
community
3. Swift syntax supports a variety of paradigms
Slide 50
Slide 50 text
Next steps
—Swift Playgrounds
—Swift on Linux
—docker-swift
—Xcode