SlideShare une entreprise Scribd logo
1  sur  267
Télécharger pour lire hors ligne
A Modest
Introduction
To Swift
KCDC 2017
4 Aug 2017
John SJ Anderson
@genehack
@genehack › Intro to Swift › KCDC 2017
Sorry!
TITANIUM	SPONSORS
Platinum	Sponsors
Gold	Sponsors
A Modest
Introduction
To Swift
KCDC 2017
4 Aug 2017
John SJ Anderson
@genehack
@genehack › Intro to Swift › KCDC 2017
Hi I'm John
aka @genehack
• VP Tech, Infinity Interactive
• Perl tribe
• Polyglot coder
• Just this guy, you know?
@genehack › Intro to Swift › KCDC 2017
Swift?
@genehack › Intro to Swift › KCDC 2017
Introduced in 2014
@genehack › Intro to Swift › KCDC 2017
Went Open Source
at version 2.2
@genehack › Intro to Swift › KCDC 2017
Version 3.1 just released
(in March)
@genehack › Intro to Swift › KCDC 2017
Version 4 coming
Real Soon Now
@genehack › Intro to Swift › KCDC 2017
Originally macOS only
@genehack › Intro to Swift › KCDC 2017
Now on Linux too.
@genehack › Intro to Swift › KCDC 2017
Android in the works!
@genehack › Intro to Swift › KCDC 2017
Android in the works!DONE!!
@genehack › Intro to Swift › KCDC 2017
Windows too?
https://swiftforwindows.github.io/
@genehack › Intro to Swift › KCDC 2017
Originally targeted macOS,
iOS, watchOS, & tvOS
@genehack › Intro to Swift › KCDC 2017
With expanded platform
support, offers potential
“single-language stack”
advantages a la Node
So what's it like, man?
First, a brief digression…
@genehack › Intro to Swift › KCDC 2017
How many macOS / iOS
users do we have here?
@genehack › Intro to Swift › KCDC 2017
How many macOS / iOS
developers do we have?
@genehack › Intro to Swift › KCDC 2017
The dirty little secret of
developing for Apple
@genehack › Intro to Swift › KCDC 2017
From In The Beginning
Was The Command Line
by Neal Stephenson
@genehack › Intro to Swift › KCDC 2017
Weird Pascal-based
naming & calling
conventions
@genehack › Intro to Swift › KCDC 2017
HANDLES?!?
@genehack › Intro to Swift › KCDC 2017
ObjectiveC’s “syntax”
Swift is the Mac of Apple
programming languages
Swift Syntax
@genehack › Intro to Swift › KCDC 2017
Comments
// this is a comment
@genehack › Intro to Swift › KCDC 2017
Comments
/* this is a
multi-line
comment */
@genehack › Intro to Swift › KCDC 2017
Comments
/* this is a
/* _nested_
multi-line comment, */
which is cool! */
@genehack › Intro to Swift › KCDC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
@genehack › Intro to Swift › KCDC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
@genehack › Intro to Swift › KCDC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
@genehack › Intro to Swift › KCDC 2017
Variables
var foo = 1
var bar: Int
var baz = "whee!"
@genehack › Intro to Swift › KCDC 2017
Variables
let bar = 1
bar += 1
// ^^ compile time error!
@genehack › Intro to Swift › KCDC 2017
Variables
let bar = 1
bar += 1
// ^^ compile time error!
@genehack › Intro to Swift › KCDC 2017
Variables
let bar
@genehack › Intro to Swift › KCDC 2017
Variables
let bar
// also a compile time error
/*
You canNOT have an uninitialized and
untyped variable. You also can't use
an uninitialized variable _at all_
*/
How friggin’ awesome is that?
@genehack › Intro to Swift › KCDC 2017
Operators
@genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let n = 1
if n > 1 {
print("we got a big N here")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
@genehack › Intro to Swift › KCDC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
@genehack › Intro to Swift › KCDC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
@genehack › Intro to Swift › KCDC 2017
Flow Control
let arr = [ 1, 2, 3, 4]
var sum = 0
for elem in arr {
sum += elem
}
// sum now is 10
@genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ... 10 {
# do something 10 times
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
for index in 1 ..< 10 {
# do something 9 times
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown--
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countDown = 5
while countDown > 0 {
countDown -= 1
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp++
} while countUp < 5
@genehack › Intro to Swift › KCDC 2017
Flow Control
var countUp = 0
repeat {
countUp += 1
} while countUp < 5
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
No fallthru by default!
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = 2
switch sample {
case 0:
print("Is 0")
case 2:
print("Is 2")
default: // mandatory when cases not exclusive
print("Not 0 or 2, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = "foo"
switch sample {
case "foo":
print("Is foo")
case "bar":
print("Is bar")
default: // mandatory when cases not exclusive
print("Not foo or bar, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = "foo"
switch sample {
case "foo":
print("Is foo")
case "bar":
print("Is bar")
default: // mandatory when cases not exclusive
print("Not foo or bar, is it.")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 2):
print("Is foo, 2")
case ("bar", _):
print("Is bar")
default: // mandatory when cases not exclusive
print(" ¯_(ツ)_/¯ ")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 3):
print("Is foo, 3")
case (let one, let two):
print("Is (one) and (two)")
}
@genehack › Intro to Swift › KCDC 2017
Flow Control
let sample = ("foo", 2)
switch sample {
case ("foo", 3):
print("Is foo, 3")
case (let one, let two):
print("Is (one) and (two)")
}
Swift is very strongly typed.
@genehack › Intro to Swift › KCDC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
@genehack › Intro to Swift › KCDC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
@genehack › Intro to Swift › KCDC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
@genehack › Intro to Swift › KCDC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
@genehack › Intro to Swift › KCDC 2017
Typing
var foo = 1 // foo is an Int
var bar: Int // bar is an uninit'd Int
var baz = Int()
if baz is Int {
print("Nice Int you got there")
}
@genehack › Intro to Swift › KCDC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
@genehack › Intro to Swift › KCDC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
@genehack › Intro to Swift › KCDC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
@genehack › Intro to Swift › KCDC 2017
Casts
var foo = 1 // foo is an Int
var bar = String(foo) // "1"
var maybeBaz = stringishThing as? String
// maybeBaz is an optionally typed String
var forceBaz = stringishThing as! String
@genehack › Intro to Swift › KCDC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
@genehack › Intro to Swift › KCDC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
@genehack › Intro to Swift › KCDC 2017
Optional Types
// When a variable may not have a value
var bar: Int?
// test
if bar != nil {
// has a value
}
@genehack › Intro to Swift › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
@genehack › Intro to Swift › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
@genehack › Intro to Swift › KCDC 2017
Optional Types
// force unwrap the value to use
if bar != nil {
bar! += 2
}
// unwrapping nil --> runtime exception!
@genehack › Intro to Swift › KCDC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
@genehack › Intro to Swift › KCDC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
@genehack › Intro to Swift › KCDC 2017
if-let
var bar: Int?
if let foo = bar {
// bar had a value &
// foo now has that unwrapped value
}
else {
// bar was nil
}
@genehack › Intro to Swift › KCDC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
@genehack › Intro to Swift › KCDC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
@genehack › Intro to Swift › KCDC 2017
if-var
var bar: Int?
if var foo = bar {
// bar had a value &
// foo now has that unwrapped value &
// foo is mutable
foo += 1
}
else {
// bar was nil
}
Types of Variables
@genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
@genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
@genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
@genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
@genehack › Intro to Swift › KCDC 2017
Tuples
let tuple = ("foo", 42)
let first = tuple.0 // "foo"
let labeledTuple = (one: "foo", two: 42)
let second = labeledTuple.two // 42
@genehack › Intro to Swift › KCDC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
@genehack › Intro to Swift › KCDC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
@genehack › Intro to Swift › KCDC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
@genehack › Intro to Swift › KCDC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
@genehack › Intro to Swift › KCDC 2017
Arrays
let nums = [1, 2, 3]
var strs : [String]
// _can_ mix & match
let mixed = [1, "foo"]
// but you probably shouldn't
// in Swift 3+ this is a compile
// error unless specifically
// type-annotated
let mixed: [Any] = [1, "foo"]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": "Missouri",
"Topeka": "Kansas"
]
// capitalCityStates has type
// [String:String]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type
// [String:Any]
@genehack › Intro to Swift › KCDC 2017
Dictionary
let capitalCityStates: [String:Any] = [
"Salem": "Oregon",
"Jeff City": 2,
"Topeka": "Kansas"
]
// capitalCityStates must be
// annotated with type [String:Any]
@genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
@genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
@genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
@genehack › Intro to Swift › KCDC 2017
Sets
var petSet :Set = [ "cat", "dog",
"fish", "dog"]
petSet.count // returns 3
Functions
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample () {
print("Hello, KCDC!")
}
// call like:
obExample()
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String) {
print("Hello, (who)!")
}
// call like
obExample(who: "KCDC 2017")
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String) {
print("Hello, (who)!")
}
// call like
obExample(who: "KCDC 2017")
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String) {
print("Hello, (who)!")
}
// call like
obExample(who: "KCDC 2017")
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String) -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample("KCDC")
// greets == "Hello, KCDC"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String) -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample("KCDC")
// greets == "Hello, KCDC"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String = "Swift") -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample(who:"KCDC")
// "Hello, KCDC!"
let defGreets = obExample()
// "Hello, Swift!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String = "Swift") -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample(who:"KCDC")
// "Hello, KCDC!"
let defGreets = obExample()
// "Hello, Swift!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String = "Swift") -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample(who:"KCDC")
// "Hello, KCDC!"
let defGreets = obExample()
// "Hello, Swift!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (who :String = "Swift") -> String {
return "Hello, (who)!"
}
// call like
let greets = obExample(who:"KCDC")
// "Hello, KCDC!"
let defGreets = obExample()
// "Hello, Swift!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
who them :String = "Swift") {
print("(what), (them)!")
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
who them :String = "Swift") {
print("(what), (them)!")
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
who them :String = "Swift") {
print("(what), (them)!")
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
who them :String = "Swift") {
print("(what), (them)!")
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
who them :String = "Swift") {
print("(what), (them)!")
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func obExample (what :String = "Hello",
_ who :String = "Swift") {
print "(what), (who)!"
}
// call like
obExample(what: "bye") // "bye, Swift!"
obExample(what: "bye", who:"Felicia") // COMPILE ERROR
obExample(what: "bye", "Felicia") // "bye, Felicia!"
@genehack › Intro to Swift › KCDC 2017
Functions
func variadiacExample (nums: Int...) {
// do something with nums
// nums is Array[Int]
}
@genehack › Intro to Swift › KCDC 2017
Functions
func variadiacExample (nums: Int...) {
// do something with nums
// nums is Array[Int]
}
Functions are first-class citizens
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
// sorted = [1, 2, 13, 32, 56, 120]
This is already the func sig for
sorted(by:)!
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// inferred param & return types
var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// inferred param & return types
var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// positionally named parameters
var sorted = numbers.sorted(by: {return $0 > $1})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// positionally named parameters
var sorted = numbers.sorted(by: {return $0 > $1})
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// when closure is last param,
// param name & parens optional
var sorted = numbers.sorted { $0 > $1 }
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
let numbers = [2,1,56,32,120,13]
// when closure is last param,
// param name & parens optional
var sorted = numbers.sorted { $0 > $1 }
// sorted = [1, 2, 13, 32, 56, 120]
@genehack › Intro to Swift › KCDC 2017
Closures
var sorted = numbers.sorted(by:{
(n1: Int, n2: Int) -> Bool in return n2 > n1
})
var sorted = numbers.sorted { $0 > $1 }
OOP is also
well supported
@genehack › Intro to Swift › KCDC 2017
Classes
class Dog {
}
@genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
@genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
@genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String
let noise = "WOOF!"
}
Class ‘Dog’ has no initializers
@genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
@genehack › Intro to Swift › KCDC 2017
Properties
class Dog {
var name: String?
let noise = "WOOF!"
}
@genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
@genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
@genehack › Intro to Swift › KCDC 2017
Initializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
}
@genehack › Intro to Swift › KCDC 2017
Deinitializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
deinit () {
// do any cleanup here
}
}
@genehack › Intro to Swift › KCDC 2017
Deinitializers
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
deinit () {
// do any cleanup here
}
}
@genehack › Intro to Swift › KCDC 2017
Methods
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
func speak () -> String {
return self.noise
}
}
@genehack › Intro to Swift › KCDC 2017
Methods
class Dog {
var name: String
let noise = "WOOF"
init (name: String) {
self.name = name
}
func speak () -> String {
return self.noise
}
}
@genehack › Intro to Swift › KCDC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
@genehack › Intro to Swift › KCDC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
@genehack › Intro to Swift › KCDC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
@genehack › Intro to Swift › KCDC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
@genehack › Intro to Swift › KCDC 2017
Using Objects
let sammy = Dog(name: "Sammy");
// sammy is Dog
print(sammy.name) // prints "Sammyn"
print(sammy.speak()) // prints "WOOF!n"
sammy.name = "Buster" // works b/c prop is var
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set {
// this is horrible, don't do this
self.birthYear = currentYear - newValue
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set (age) {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set (age) {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
get {
return currentYear - self.birthYear
}
set (age) {
// this is horrible, don't do this
self.birthYear = currentYear - age
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
willSet {
// runs before property value changes
}
didSet {
// runs after property value changes
}
}
}
@genehack › Intro to Swift › KCDC 2017
Computed Properties
class Dog {
var age :Int {
willSet {
// runs before property value changes
}
didSet {
// runs after property value changes
}
}
}
Inheritance
@genehack › Intro to Swift › KCDC 2017
Inheritance
class Animal {
}
class Dog : Animal {
}
@genehack › Intro to Swift › KCDC 2017
Inheritance
class Animal {
}
class Dog : Animal {
}
@genehack › Intro to Swift › KCDC 2017
Inheritance
class Animal {
let name: String
init (name: name) {
self.name = name
}
}
class Dog : Animal {
override init (name: name) {
super.init(name: name)
}
}
@genehack › Intro to Swift › KCDC 2017
Inheritance
class Animal {
let name: String
init (name: name) {
self.name = name
}
}
class Dog : Animal {
override init (name: name) {
super.init(name: name)
}
}
@genehack › Intro to Swift › KCDC 2017
Inheritance
class Animal {
let name: String
init (name: name) {
self.name = name
}
}
class Dog : Animal {
override init (name: name) {
super.init(name: name)
}
}
@genehack › Intro to Swift › KCDC 2017
Overrides
class Animal {
func speak() { ... }
}
class Dog : Animal {
override func speak () { ... }
}
@genehack › Intro to Swift › KCDC 2017
Overrides
class Animal {
func speak() { ... }
}
class Dog : Animal {
override func speak () { ... }
}
Structs & Enumerations
@genehack › Intro to Swift › KCDC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
@genehack › Intro to Swift › KCDC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
@genehack › Intro to Swift › KCDC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
@genehack › Intro to Swift › KCDC 2017
Structs
struct Animal {
var name: String
var noise: String
init (name: String, makes: String) {
self.name = name
noise = makes
}
func speak () -> String {
return noise
}
}
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
}
var conf = OpenSourceConfs.KCDC
// conf is type OpenSourceConfs
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs :Int {
case KCDC = 1
case OpenWest
}
var conf = OpenSourceConfs.OpenWest
// conf is type OpenSourceConfs
conf.rawValue // 2
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC
case OpenWest
func describe() -> String {
switch self {
case .KCDC:
return "Hello Kansas City!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC(let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var conf = OpenSourceConfs.KCDC("Olathe")
conf.describe()
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC (let location):
return "Hello (location)!"
case .OpenWest:
return "Hello Salt Lake City!"
}
}
}
var kcdc2017 = OpenSourceConfs.KCDC("KC")
var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
@genehack › Intro to Swift › KCDC 2017
Enumerations
enum OpenSourceConfs {
case KCDC(String)
case OpenWest
func describe() -> String {
switch self {
case .KCDC (let location):
return "Hello (location)!"
case .OpenWest
return "Hello Salt Lake City!"
}
}
}
var kcdc2017 = OpenSourceConfs.KCDC("KC")
var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker {
var noise: String { get }
func talk () -> String
mutating func mute()
}
class Dog: Talker {
var noise: String
init (noise: String) {
self.noise = noise
}
func talk () -> String {
return noise
}
func mute () {
noise = ""
}
}
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker { ... }
class Dog: Talker { ... }
class Fish { ... }
var sammy: Talker
sammy = Dog(noise: "WOOF!")
sammy = Fish() // compile error
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker { ... }
class Dog: Talker { ... }
class Fish { ... }
var sammy: Talker
sammy = Dog(noise: "WOOF!")
sammy = Fish() // compile error
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker { ... }
class Dog: Talker { ... }
class Fish { ... }
var sammy: Talker
sammy = Dog(noise: "WOOF!")
sammy = Fish() // compile error
@genehack › Intro to Swift › KCDC 2017
Protocols
protocol Talker { ... }
class Dog: Talker { ... }
class Fish { ... }
var sammy: Talker
sammy = Dog(noise: "WOOF!")
sammy = Fish() // compile error
@genehack › Intro to Swift › KCDC 2017
Extensions
extension Int {
func squared () -> Int {
return self * self
}
}
let foo = 2
print(foo.squared()) // 4
print(foo.squared().squared()) // 16
@genehack › Intro to Swift › KCDC 2017
Extensions
extension Int {
func squared () -> Int {
return self * self
}
}
let foo = 2
print(foo.squared()) // 4
print(foo.squared().squared()) // 16
@genehack › Intro to Swift › KCDC 2017
Extensions
extension Int {
func squared () -> Int {
return self * self
}
}
let foo = 2
print(foo.squared()) // 4
print(foo.squared().squared()) // 16
@genehack › Intro to Swift › KCDC 2017
Extensions
extension Int {
func squared () -> Int {
return self * self
}
}
let foo = 2
print(foo.squared()) // 4
print(foo.squared().squared()) // 16
Exceptions & Error Handling
@genehack › Intro to Swift › KCDC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
enum TalkErrors: Error {
case TooShort
case TooLong
case TooBoring
}
func giveATalk (talk: String) throws -> String {
if talk == "boring" {
throw TalkErrors.TooBoring
}
return "talk!"
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "boring")
print(thisTalk)
}
catch {
print(error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
do {
let thisTalk = try giveATalk(talk: "fine")
print(thisTalk)
}
catch TalkErrors.TooLong {
print("shut up already")
}
catch let talkError as TalkErrors {
print("Talk error: (talkError).")
}
catch {
print (error)
}
@genehack › Intro to Swift › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
@genehack › Intro to Swift › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
@genehack › Intro to Swift › KCDC 2017
Exceptions
// silently discards error
let thisTalk = try? giveATalk(talk:"fine")
// thisTalk isa String?
@genehack › Intro to Swift › KCDC 2017
Defer
func needsMuchSetupAndTearDown () {
// do the setup here, open files, etc.
defer {
// and do the cleanup here,
// right next to set up
}
// other code here.
}
@genehack › Intro to Swift › KCDC 2017
Defer
func needsMuchSetupAndTearDown () {
// do the setup here, open files, etc.
defer {
// and do the cleanup here,
// right next to set up
}
// other code here.
}
Workspaces
Ill-advised
live
demo
time!
Ill-advised
live
demo
time!
https://developer.apple.com/swift
thanks!
GIMME YR TALKS CFP OPEN NOW!
Oct 6-7
seagl.org
questions?

Contenu connexe

Tendances

REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...Vladimir Dejanovic
 
libinjection: a C library for SQLi detection, from Black Hat USA 2012
libinjection: a C library for SQLi detection, from Black Hat USA 2012libinjection: a C library for SQLi detection, from Black Hat USA 2012
libinjection: a C library for SQLi detection, from Black Hat USA 2012Nick Galbreath
 
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk editionAlex Borysov
 
common mistakes when using libcurl
common mistakes when using libcurlcommon mistakes when using libcurl
common mistakes when using libcurlDaniel Stenberg
 
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019Alex Borysov
 
"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 edition"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 editionAlex Borysov
 
Brian hogg word camp preparing a plugin for translation
Brian hogg   word camp preparing a plugin for translationBrian hogg   word camp preparing a plugin for translation
Brian hogg word camp preparing a plugin for translationwcto2017
 
HTTP/3 is next generation HTTP
HTTP/3 is next generation HTTPHTTP/3 is next generation HTTP
HTTP/3 is next generation HTTPDaniel Stenberg
 
"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition
"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition
"Enabling Googley microservices with gRPC" Riga DevDays 2018 editionAlex Borysov
 
Preparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationPreparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationBrian Hogg
 
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...Alex Borysov
 
libinjection and sqli obfuscation, presented at OWASP NYC
libinjection and sqli obfuscation, presented at OWASP NYClibinjection and sqli obfuscation, presented at OWASP NYC
libinjection and sqli obfuscation, presented at OWASP NYCNick Galbreath
 
Testing curl for security
Testing curl for securityTesting curl for security
Testing curl for securityDaniel Stenberg
 
Inspec one tool to rule them all
Inspec one tool to rule them allInspec one tool to rule them all
Inspec one tool to rule them allKimball Johnson
 
Break me if you can: practical guide to building fault-tolerant systems (with...
Break me if you can: practical guide to building fault-tolerant systems (with...Break me if you can: practical guide to building fault-tolerant systems (with...
Break me if you can: practical guide to building fault-tolerant systems (with...Alex Borysov
 

Tendances (18)

REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
 
Curl with rust
Curl with rustCurl with rust
Curl with rust
 
libinjection: a C library for SQLi detection, from Black Hat USA 2012
libinjection: a C library for SQLi detection, from Black Hat USA 2012libinjection: a C library for SQLi detection, from Black Hat USA 2012
libinjection: a C library for SQLi detection, from Black Hat USA 2012
 
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
"Enabling Googley microservices with gRPC" VoxxedDays Minsk edition
 
common mistakes when using libcurl
common mistakes when using libcurlcommon mistakes when using libcurl
common mistakes when using libcurl
 
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019"gRPC-Web:  It’s All About Communication": Devoxx Ukraine 2019
"gRPC-Web: It’s All About Communication": Devoxx Ukraine 2019
 
"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 edition"gRPC vs REST: let the battle begin!" OSCON 2018 edition
"gRPC vs REST: let the battle begin!" OSCON 2018 edition
 
Brian hogg word camp preparing a plugin for translation
Brian hogg   word camp preparing a plugin for translationBrian hogg   word camp preparing a plugin for translation
Brian hogg word camp preparing a plugin for translation
 
HTTP/3 is next generation HTTP
HTTP/3 is next generation HTTPHTTP/3 is next generation HTTP
HTTP/3 is next generation HTTP
 
"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition
"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition
"Enabling Googley microservices with gRPC" Riga DevDays 2018 edition
 
Preparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for TranslationPreparing a WordPress Plugin for Translation
Preparing a WordPress Plugin for Translation
 
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...
DevNexus 2020 "Break me if you can: practical guide to building fault-toleran...
 
libinjection and sqli obfuscation, presented at OWASP NYC
libinjection and sqli obfuscation, presented at OWASP NYClibinjection and sqli obfuscation, presented at OWASP NYC
libinjection and sqli obfuscation, presented at OWASP NYC
 
JavaLand gRPC vs REST API
JavaLand gRPC vs REST APIJavaLand gRPC vs REST API
JavaLand gRPC vs REST API
 
Testing curl for security
Testing curl for securityTesting curl for security
Testing curl for security
 
HTTP/3 in curl
HTTP/3 in curlHTTP/3 in curl
HTTP/3 in curl
 
Inspec one tool to rule them all
Inspec one tool to rule them allInspec one tool to rule them all
Inspec one tool to rule them all
 
Break me if you can: practical guide to building fault-tolerant systems (with...
Break me if you can: practical guide to building fault-tolerant systems (with...Break me if you can: practical guide to building fault-tolerant systems (with...
Break me if you can: practical guide to building fault-tolerant systems (with...
 

Similaire à A Modest Introduction to Swift

A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to SwiftAll Things Open
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To SwiftJohn Anderson
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To SwiftJohn Anderson
 
Hands-on VeriFast with STM32 microcontroller @ Osaka
Hands-on VeriFast with STM32 microcontroller @ OsakaHands-on VeriFast with STM32 microcontroller @ Osaka
Hands-on VeriFast with STM32 microcontroller @ OsakaKiwamu Okabe
 
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...PROIDEA
 
ATS Programming Tutorial
ATS Programming TutorialATS Programming Tutorial
ATS Programming TutorialKiwamu Okabe
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side SwiftChad Moone
 
C#7, 7.1, 7.2, 7.3 e C# 8
C#7, 7.1, 7.2, 7.3 e C# 8C#7, 7.1, 7.2, 7.3 e C# 8
C#7, 7.1, 7.2, 7.3 e C# 8Giovanni Bassi
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Carl Brown
 
Hands-on VeriFast with STM32 microcontroller @ Nagoya
Hands-on VeriFast with STM32 microcontroller @ NagoyaHands-on VeriFast with STM32 microcontroller @ Nagoya
Hands-on VeriFast with STM32 microcontroller @ NagoyaKiwamu Okabe
 
Spark Streaming @ Berlin Apache Spark Meetup, March 2015
Spark Streaming @ Berlin Apache Spark Meetup, March 2015Spark Streaming @ Berlin Apache Spark Meetup, March 2015
Spark Streaming @ Berlin Apache Spark Meetup, March 2015Stratio
 
The LLDB Debugger in FreeBSD by Ed Maste
The LLDB Debugger in FreeBSD by Ed MasteThe LLDB Debugger in FreeBSD by Ed Maste
The LLDB Debugger in FreeBSD by Ed Masteeurobsdcon
 
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...Databricks
 
Metasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project OverviewMetasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project OverviewKiwamu Okabe
 
Metasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUMetasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUKiwamu Okabe
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Eric Phan
 

Similaire à A Modest Introduction to Swift (20)

A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to Swift
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To Swift
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To Swift
 
Hands-on VeriFast with STM32 microcontroller @ Osaka
Hands-on VeriFast with STM32 microcontroller @ OsakaHands-on VeriFast with STM32 microcontroller @ Osaka
Hands-on VeriFast with STM32 microcontroller @ Osaka
 
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
Atmosphere Conference 2015: Need for Async: In pursuit of scalable internet-s...
 
ATS Programming Tutorial
ATS Programming TutorialATS Programming Tutorial
ATS Programming Tutorial
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
C#7, 7.1, 7.2, 7.3 e C# 8
C#7, 7.1, 7.2, 7.3 e C# 8C#7, 7.1, 7.2, 7.3 e C# 8
C#7, 7.1, 7.2, 7.3 e C# 8
 
Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016Swift 2.2 Design Patterns CocoaConf Austin 2016
Swift 2.2 Design Patterns CocoaConf Austin 2016
 
Hands-on VeriFast with STM32 microcontroller @ Nagoya
Hands-on VeriFast with STM32 microcontroller @ NagoyaHands-on VeriFast with STM32 microcontroller @ Nagoya
Hands-on VeriFast with STM32 microcontroller @ Nagoya
 
Spark Streaming @ Berlin Apache Spark Meetup, March 2015
Spark Streaming @ Berlin Apache Spark Meetup, March 2015Spark Streaming @ Berlin Apache Spark Meetup, March 2015
Spark Streaming @ Berlin Apache Spark Meetup, March 2015
 
Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 
The LLDB Debugger in FreeBSD by Ed Maste
The LLDB Debugger in FreeBSD by Ed MasteThe LLDB Debugger in FreeBSD by Ed Maste
The LLDB Debugger in FreeBSD by Ed Maste
 
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...
Cassandra and SparkSQL: You Don't Need Functional Programming for Fun with Ru...
 
Metasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project OverviewMetasepi team meeting: Ajhc Project Overview
Metasepi team meeting: Ajhc Project Overview
 
Cassandra and Spark SQL
Cassandra and Spark SQLCassandra and Spark SQL
Cassandra and Spark SQL
 
The Rust Borrow Checker
The Rust Borrow CheckerThe Rust Borrow Checker
The Rust Borrow Checker
 
Metasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUMetasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCU
 
Jquery2012 defs
Jquery2012 defsJquery2012 defs
Jquery2012 defs
 
Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!Why you should be using the shiny new C# 6.0 features now!
Why you should be using the shiny new C# 6.0 features now!
 

Plus de John Anderson

Introduction to Git (even for non-developers)
Introduction to Git (even for non-developers)Introduction to Git (even for non-developers)
Introduction to Git (even for non-developers)John Anderson
 
Logs are-magic-devfestweekend2018
Logs are-magic-devfestweekend2018Logs are-magic-devfestweekend2018
Logs are-magic-devfestweekend2018John Anderson
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouJohn Anderson
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning projectJohn Anderson
 
Do you want to be right or do you want to WIN?
Do you want to be right or do you want to WIN?Do you want to be right or do you want to WIN?
Do you want to be right or do you want to WIN?John Anderson
 
An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)John Anderson
 
You got chocolate in my peanut butter! .NET on Mac & Linux
You got chocolate in my peanut butter! .NET on Mac & LinuxYou got chocolate in my peanut butter! .NET on Mac & Linux
You got chocolate in my peanut butter! .NET on Mac & LinuxJohn Anderson
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning projectJohn Anderson
 
Old Dogs & New Tricks: What's New with Perl5 This Century
Old Dogs & New Tricks: What's New with Perl5 This CenturyOld Dogs & New Tricks: What's New with Perl5 This Century
Old Dogs & New Tricks: What's New with Perl5 This CenturyJohn Anderson
 
Introduction to Git (even for non-developers!)
Introduction to Git (even for non-developers!)Introduction to Git (even for non-developers!)
Introduction to Git (even for non-developers!)John Anderson
 
Introduction to Git for Non-Developers
Introduction to Git for Non-DevelopersIntroduction to Git for Non-Developers
Introduction to Git for Non-DevelopersJohn Anderson
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning projectJohn Anderson
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouJohn Anderson
 
JSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJohn Anderson
 
Old Dogs & New Tricks: What's New With Perl5 This Century
Old Dogs & New Tricks: What's New With Perl5 This CenturyOld Dogs & New Tricks: What's New With Perl5 This Century
Old Dogs & New Tricks: What's New With Perl5 This CenturyJohn Anderson
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouJohn Anderson
 
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...John Anderson
 
Logs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youLogs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youJohn Anderson
 

Plus de John Anderson (20)

#speakerlife
#speakerlife#speakerlife
#speakerlife
 
Introduction to Git (even for non-developers)
Introduction to Git (even for non-developers)Introduction to Git (even for non-developers)
Introduction to Git (even for non-developers)
 
Logs are-magic-devfestweekend2018
Logs are-magic-devfestweekend2018Logs are-magic-devfestweekend2018
Logs are-magic-devfestweekend2018
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning project
 
Do you want to be right or do you want to WIN?
Do you want to be right or do you want to WIN?Do you want to be right or do you want to WIN?
Do you want to be right or do you want to WIN?
 
An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)An Introduction to Git (even for non-developers)
An Introduction to Git (even for non-developers)
 
You got chocolate in my peanut butter! .NET on Mac & Linux
You got chocolate in my peanut butter! .NET on Mac & LinuxYou got chocolate in my peanut butter! .NET on Mac & Linux
You got chocolate in my peanut butter! .NET on Mac & Linux
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning project
 
Old Dogs & New Tricks: What's New with Perl5 This Century
Old Dogs & New Tricks: What's New with Perl5 This CenturyOld Dogs & New Tricks: What's New with Perl5 This Century
Old Dogs & New Tricks: What's New with Perl5 This Century
 
Introduction to Git (even for non-developers!)
Introduction to Git (even for non-developers!)Introduction to Git (even for non-developers!)
Introduction to Git (even for non-developers!)
 
Introduction to Git for Non-Developers
Introduction to Git for Non-DevelopersIntroduction to Git for Non-Developers
Introduction to Git for Non-Developers
 
A static site generator should be your next language learning project
A static site generator should be your next language learning projectA static site generator should be your next language learning project
A static site generator should be your next language learning project
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
 
JSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your LifeJSON Web Tokens Will Improve Your Life
JSON Web Tokens Will Improve Your Life
 
Old Dogs & New Tricks: What's New With Perl5 This Century
Old Dogs & New Tricks: What's New With Perl5 This CenturyOld Dogs & New Tricks: What's New With Perl5 This Century
Old Dogs & New Tricks: What's New With Perl5 This Century
 
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To YouLogs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
Logs Are Magic: Why Git Workflows and Commit Structure Should Matter To You
 
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
Friends Don't Let Friends Browse Unencrypted: Running a VPN for friends and f...
 
Logs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youLogs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to you
 
#speakerlife
#speakerlife#speakerlife
#speakerlife
 

Dernier

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 

Dernier (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 

A Modest Introduction to Swift

  • 1. A Modest Introduction To Swift KCDC 2017 4 Aug 2017 John SJ Anderson @genehack
  • 2. @genehack › Intro to Swift › KCDC 2017 Sorry!
  • 4.
  • 5. A Modest Introduction To Swift KCDC 2017 4 Aug 2017 John SJ Anderson @genehack
  • 6. @genehack › Intro to Swift › KCDC 2017 Hi I'm John aka @genehack • VP Tech, Infinity Interactive • Perl tribe • Polyglot coder • Just this guy, you know?
  • 7.
  • 8. @genehack › Intro to Swift › KCDC 2017 Swift?
  • 9. @genehack › Intro to Swift › KCDC 2017 Introduced in 2014
  • 10. @genehack › Intro to Swift › KCDC 2017 Went Open Source at version 2.2
  • 11. @genehack › Intro to Swift › KCDC 2017 Version 3.1 just released (in March)
  • 12. @genehack › Intro to Swift › KCDC 2017 Version 4 coming Real Soon Now
  • 13. @genehack › Intro to Swift › KCDC 2017 Originally macOS only
  • 14. @genehack › Intro to Swift › KCDC 2017 Now on Linux too.
  • 15.
  • 16. @genehack › Intro to Swift › KCDC 2017 Android in the works!
  • 17.
  • 18.
  • 19. @genehack › Intro to Swift › KCDC 2017 Android in the works!DONE!!
  • 20. @genehack › Intro to Swift › KCDC 2017 Windows too?
  • 22. @genehack › Intro to Swift › KCDC 2017 Originally targeted macOS, iOS, watchOS, & tvOS
  • 23. @genehack › Intro to Swift › KCDC 2017 With expanded platform support, offers potential “single-language stack” advantages a la Node
  • 24. So what's it like, man?
  • 25. First, a brief digression…
  • 26. @genehack › Intro to Swift › KCDC 2017 How many macOS / iOS users do we have here?
  • 27. @genehack › Intro to Swift › KCDC 2017 How many macOS / iOS developers do we have?
  • 28. @genehack › Intro to Swift › KCDC 2017 The dirty little secret of developing for Apple
  • 29. @genehack › Intro to Swift › KCDC 2017 From In The Beginning Was The Command Line by Neal Stephenson
  • 30. @genehack › Intro to Swift › KCDC 2017 Weird Pascal-based naming & calling conventions
  • 31. @genehack › Intro to Swift › KCDC 2017 HANDLES?!?
  • 32. @genehack › Intro to Swift › KCDC 2017 ObjectiveC’s “syntax”
  • 33. Swift is the Mac of Apple programming languages
  • 35. @genehack › Intro to Swift › KCDC 2017 Comments // this is a comment
  • 36. @genehack › Intro to Swift › KCDC 2017 Comments /* this is a multi-line comment */
  • 37. @genehack › Intro to Swift › KCDC 2017 Comments /* this is a /* _nested_ multi-line comment, */ which is cool! */
  • 38. @genehack › Intro to Swift › KCDC 2017 Variables var foo = 1 var bar: Int var baz = "whee!"
  • 39. @genehack › Intro to Swift › KCDC 2017 Variables var foo = 1 var bar: Int var baz = "whee!"
  • 40. @genehack › Intro to Swift › KCDC 2017 Variables var foo = 1 var bar: Int var baz = "whee!"
  • 41. @genehack › Intro to Swift › KCDC 2017 Variables var foo = 1 var bar: Int var baz = "whee!"
  • 42. @genehack › Intro to Swift › KCDC 2017 Variables let bar = 1 bar += 1 // ^^ compile time error!
  • 43. @genehack › Intro to Swift › KCDC 2017 Variables let bar = 1 bar += 1 // ^^ compile time error!
  • 44. @genehack › Intro to Swift › KCDC 2017 Variables let bar
  • 45. @genehack › Intro to Swift › KCDC 2017 Variables let bar // also a compile time error /* You canNOT have an uninitialized and untyped variable. You also can't use an uninitialized variable _at all_ */
  • 47. @genehack › Intro to Swift › KCDC 2017 Operators
  • 48. @genehack › Intro to Swift › KCDC 2017 Flow Control let n = 1 if n > 1 { print("we got a big N here") }
  • 49. @genehack › Intro to Swift › KCDC 2017 Flow Control let n = 1 if n > 1 { print("we got a big N here") }
  • 50. @genehack › Intro to Swift › KCDC 2017 Flow Control let n = 1 if n > 1 { print("we got a big N here") }
  • 51. @genehack › Intro to Swift › KCDC 2017 Flow Control let arr = [ 1, 2, 3, 4] var sum = 0 for elem in arr { sum += elem } // sum now is 10
  • 52. @genehack › Intro to Swift › KCDC 2017 Flow Control let arr = [ 1, 2, 3, 4] var sum = 0 for elem in arr { sum += elem } // sum now is 10
  • 53. @genehack › Intro to Swift › KCDC 2017 Flow Control let arr = [ 1, 2, 3, 4] var sum = 0 for elem in arr { sum += elem } // sum now is 10
  • 54. @genehack › Intro to Swift › KCDC 2017 Flow Control let arr = [ 1, 2, 3, 4] var sum = 0 for elem in arr { sum += elem } // sum now is 10
  • 55. @genehack › Intro to Swift › KCDC 2017 Flow Control for index in 1 ... 10 { # do something 10 times }
  • 56. @genehack › Intro to Swift › KCDC 2017 Flow Control for index in 1 ... 10 { # do something 10 times }
  • 57. @genehack › Intro to Swift › KCDC 2017 Flow Control for index in 1 ..< 10 { # do something 9 times }
  • 58. @genehack › Intro to Swift › KCDC 2017 Flow Control for index in 1 ..< 10 { # do something 9 times }
  • 59. @genehack › Intro to Swift › KCDC 2017 Flow Control var countDown = 5 while countDown > 0 { countDown-- }
  • 60. @genehack › Intro to Swift › KCDC 2017 Flow Control var countDown = 5 while countDown > 0 { countDown-- }
  • 61. @genehack › Intro to Swift › KCDC 2017 Flow Control var countDown = 5 while countDown > 0 { countDown-- }
  • 62. @genehack › Intro to Swift › KCDC 2017 Flow Control var countDown = 5 while countDown > 0 { countDown -= 1 }
  • 63. @genehack › Intro to Swift › KCDC 2017 Flow Control var countUp = 0 repeat { countUp++ } while countUp < 5
  • 64. @genehack › Intro to Swift › KCDC 2017 Flow Control var countUp = 0 repeat { countUp++ } while countUp < 5
  • 65. @genehack › Intro to Swift › KCDC 2017 Flow Control var countUp = 0 repeat { countUp++ } while countUp < 5
  • 66. @genehack › Intro to Swift › KCDC 2017 Flow Control var countUp = 0 repeat { countUp += 1 } while countUp < 5
  • 67. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = 2 switch sample { case 0: print("Is 0") case 2: print("Is 2") default: // mandatory when cases not exclusive print("Not 0 or 2, is it.") }
  • 68. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = 2 switch sample { case 0: print("Is 0") case 2: print("Is 2") default: // mandatory when cases not exclusive print("Not 0 or 2, is it.") }
  • 69. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = 2 switch sample { case 0: print("Is 0") case 2: print("Is 2") default: // mandatory when cases not exclusive print("Not 0 or 2, is it.") }
  • 70. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = 2 switch sample { case 0: print("Is 0") case 2: print("Is 2") default: // mandatory when cases not exclusive print("Not 0 or 2, is it.") } No fallthru by default!
  • 71. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = 2 switch sample { case 0: print("Is 0") case 2: print("Is 2") default: // mandatory when cases not exclusive print("Not 0 or 2, is it.") }
  • 72. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = "foo" switch sample { case "foo": print("Is foo") case "bar": print("Is bar") default: // mandatory when cases not exclusive print("Not foo or bar, is it.") }
  • 73. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = "foo" switch sample { case "foo": print("Is foo") case "bar": print("Is bar") default: // mandatory when cases not exclusive print("Not foo or bar, is it.") }
  • 74. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 2): print("Is foo, 2") case ("bar", _): print("Is bar") default: // mandatory when cases not exclusive print(" ¯_(ツ)_/¯ ") }
  • 75. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 2): print("Is foo, 2") case ("bar", _): print("Is bar") default: // mandatory when cases not exclusive print(" ¯_(ツ)_/¯ ") }
  • 76. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 2): print("Is foo, 2") case ("bar", _): print("Is bar") default: // mandatory when cases not exclusive print(" ¯_(ツ)_/¯ ") }
  • 77. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 2): print("Is foo, 2") case ("bar", _): print("Is bar") default: // mandatory when cases not exclusive print(" ¯_(ツ)_/¯ ") }
  • 78. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 3): print("Is foo, 3") case (let one, let two): print("Is (one) and (two)") }
  • 79. @genehack › Intro to Swift › KCDC 2017 Flow Control let sample = ("foo", 2) switch sample { case ("foo", 3): print("Is foo, 3") case (let one, let two): print("Is (one) and (two)") }
  • 80. Swift is very strongly typed.
  • 81. @genehack › Intro to Swift › KCDC 2017 Typing var foo = 1 // foo is an Int var bar: Int // bar is an uninit'd Int var baz = Int() if baz is Int { print("Nice Int you got there") }
  • 82. @genehack › Intro to Swift › KCDC 2017 Typing var foo = 1 // foo is an Int var bar: Int // bar is an uninit'd Int var baz = Int() if baz is Int { print("Nice Int you got there") }
  • 83. @genehack › Intro to Swift › KCDC 2017 Typing var foo = 1 // foo is an Int var bar: Int // bar is an uninit'd Int var baz = Int() if baz is Int { print("Nice Int you got there") }
  • 84. @genehack › Intro to Swift › KCDC 2017 Typing var foo = 1 // foo is an Int var bar: Int // bar is an uninit'd Int var baz = Int() if baz is Int { print("Nice Int you got there") }
  • 85. @genehack › Intro to Swift › KCDC 2017 Typing var foo = 1 // foo is an Int var bar: Int // bar is an uninit'd Int var baz = Int() if baz is Int { print("Nice Int you got there") }
  • 86. @genehack › Intro to Swift › KCDC 2017 Casts var foo = 1 // foo is an Int var bar = String(foo) // "1" var maybeBaz = stringishThing as? String // maybeBaz is an optionally typed String var forceBaz = stringishThing as! String
  • 87. @genehack › Intro to Swift › KCDC 2017 Casts var foo = 1 // foo is an Int var bar = String(foo) // "1" var maybeBaz = stringishThing as? String // maybeBaz is an optionally typed String var forceBaz = stringishThing as! String
  • 88. @genehack › Intro to Swift › KCDC 2017 Casts var foo = 1 // foo is an Int var bar = String(foo) // "1" var maybeBaz = stringishThing as? String // maybeBaz is an optionally typed String var forceBaz = stringishThing as! String
  • 89. @genehack › Intro to Swift › KCDC 2017 Casts var foo = 1 // foo is an Int var bar = String(foo) // "1" var maybeBaz = stringishThing as? String // maybeBaz is an optionally typed String var forceBaz = stringishThing as! String
  • 90. @genehack › Intro to Swift › KCDC 2017 Optional Types // When a variable may not have a value var bar: Int? // test if bar != nil { // has a value }
  • 91. @genehack › Intro to Swift › KCDC 2017 Optional Types // When a variable may not have a value var bar: Int? // test if bar != nil { // has a value }
  • 92. @genehack › Intro to Swift › KCDC 2017 Optional Types // When a variable may not have a value var bar: Int? // test if bar != nil { // has a value }
  • 93. @genehack › Intro to Swift › KCDC 2017 Optional Types // force unwrap the value to use if bar != nil { bar! += 2 } // unwrapping nil --> runtime exception!
  • 94. @genehack › Intro to Swift › KCDC 2017 Optional Types // force unwrap the value to use if bar != nil { bar! += 2 } // unwrapping nil --> runtime exception!
  • 95. @genehack › Intro to Swift › KCDC 2017 Optional Types // force unwrap the value to use if bar != nil { bar! += 2 } // unwrapping nil --> runtime exception!
  • 96. @genehack › Intro to Swift › KCDC 2017 if-let var bar: Int? if let foo = bar { // bar had a value & // foo now has that unwrapped value } else { // bar was nil }
  • 97. @genehack › Intro to Swift › KCDC 2017 if-let var bar: Int? if let foo = bar { // bar had a value & // foo now has that unwrapped value } else { // bar was nil }
  • 98. @genehack › Intro to Swift › KCDC 2017 if-let var bar: Int? if let foo = bar { // bar had a value & // foo now has that unwrapped value } else { // bar was nil }
  • 99. @genehack › Intro to Swift › KCDC 2017 if-var var bar: Int? if var foo = bar { // bar had a value & // foo now has that unwrapped value & // foo is mutable foo += 1 } else { // bar was nil }
  • 100. @genehack › Intro to Swift › KCDC 2017 if-var var bar: Int? if var foo = bar { // bar had a value & // foo now has that unwrapped value & // foo is mutable foo += 1 } else { // bar was nil }
  • 101. @genehack › Intro to Swift › KCDC 2017 if-var var bar: Int? if var foo = bar { // bar had a value & // foo now has that unwrapped value & // foo is mutable foo += 1 } else { // bar was nil }
  • 103. @genehack › Intro to Swift › KCDC 2017 Tuples let tuple = ("foo", 42) let first = tuple.0 // "foo" let labeledTuple = (one: "foo", two: 42) let second = labeledTuple.two // 42
  • 104. @genehack › Intro to Swift › KCDC 2017 Tuples let tuple = ("foo", 42) let first = tuple.0 // "foo" let labeledTuple = (one: "foo", two: 42) let second = labeledTuple.two // 42
  • 105. @genehack › Intro to Swift › KCDC 2017 Tuples let tuple = ("foo", 42) let first = tuple.0 // "foo" let labeledTuple = (one: "foo", two: 42) let second = labeledTuple.two // 42
  • 106. @genehack › Intro to Swift › KCDC 2017 Tuples let tuple = ("foo", 42) let first = tuple.0 // "foo" let labeledTuple = (one: "foo", two: 42) let second = labeledTuple.two // 42
  • 107. @genehack › Intro to Swift › KCDC 2017 Tuples let tuple = ("foo", 42) let first = tuple.0 // "foo" let labeledTuple = (one: "foo", two: 42) let second = labeledTuple.two // 42
  • 108. @genehack › Intro to Swift › KCDC 2017 Arrays let nums = [1, 2, 3] var strs : [String] // _can_ mix & match let mixed = [1, "foo"] // but you probably shouldn't // in Swift 3+ this is a compile // error unless specifically // type-annotated let mixed: [Any] = [1, "foo"]
  • 109. @genehack › Intro to Swift › KCDC 2017 Arrays let nums = [1, 2, 3] var strs : [String] // _can_ mix & match let mixed = [1, "foo"] // but you probably shouldn't // in Swift 3+ this is a compile // error unless specifically // type-annotated let mixed: [Any] = [1, "foo"]
  • 110. @genehack › Intro to Swift › KCDC 2017 Arrays let nums = [1, 2, 3] var strs : [String] // _can_ mix & match let mixed = [1, "foo"] // but you probably shouldn't // in Swift 3+ this is a compile // error unless specifically // type-annotated let mixed: [Any] = [1, "foo"]
  • 111. @genehack › Intro to Swift › KCDC 2017 Arrays let nums = [1, 2, 3] var strs : [String] // _can_ mix & match let mixed = [1, "foo"] // but you probably shouldn't // in Swift 3+ this is a compile // error unless specifically // type-annotated let mixed: [Any] = [1, "foo"]
  • 112. @genehack › Intro to Swift › KCDC 2017 Arrays let nums = [1, 2, 3] var strs : [String] // _can_ mix & match let mixed = [1, "foo"] // but you probably shouldn't // in Swift 3+ this is a compile // error unless specifically // type-annotated let mixed: [Any] = [1, "foo"]
  • 113. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": "Missouri", "Topeka": "Kansas" ] // capitalCityStates has type // [String:String]
  • 114. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": "Missouri", "Topeka": "Kansas" ] // capitalCityStates has type // [String:String]
  • 115. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": "Missouri", "Topeka": "Kansas" ] // capitalCityStates has type // [String:String]
  • 116. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": 2, "Topeka": "Kansas" ] // capitalCityStates must be // annotated with type // [String:Any]
  • 117. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": 2, "Topeka": "Kansas" ] // capitalCityStates must be // annotated with type // [String:Any]
  • 118. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates = [ "Salem": "Oregon", "Jeff City": 2, "Topeka": "Kansas" ] // capitalCityStates must be // annotated with type // [String:Any]
  • 119. @genehack › Intro to Swift › KCDC 2017 Dictionary let capitalCityStates: [String:Any] = [ "Salem": "Oregon", "Jeff City": 2, "Topeka": "Kansas" ] // capitalCityStates must be // annotated with type [String:Any]
  • 120. @genehack › Intro to Swift › KCDC 2017 Sets var petSet :Set = [ "cat", "dog", "fish", "dog"] petSet.count // returns 3
  • 121. @genehack › Intro to Swift › KCDC 2017 Sets var petSet :Set = [ "cat", "dog", "fish", "dog"] petSet.count // returns 3
  • 122. @genehack › Intro to Swift › KCDC 2017 Sets var petSet :Set = [ "cat", "dog", "fish", "dog"] petSet.count // returns 3
  • 123. @genehack › Intro to Swift › KCDC 2017 Sets var petSet :Set = [ "cat", "dog", "fish", "dog"] petSet.count // returns 3
  • 125. @genehack › Intro to Swift › KCDC 2017 Functions func obExample () { print("Hello, KCDC!") } // call like: obExample()
  • 126. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String) { print("Hello, (who)!") } // call like obExample(who: "KCDC 2017")
  • 127. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String) { print("Hello, (who)!") } // call like obExample(who: "KCDC 2017")
  • 128. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String) { print("Hello, (who)!") } // call like obExample(who: "KCDC 2017")
  • 129. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String) -> String { return "Hello, (who)!" } // call like let greets = obExample("KCDC") // greets == "Hello, KCDC"
  • 130. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String) -> String { return "Hello, (who)!" } // call like let greets = obExample("KCDC") // greets == "Hello, KCDC"
  • 131. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String = "Swift") -> String { return "Hello, (who)!" } // call like let greets = obExample(who:"KCDC") // "Hello, KCDC!" let defGreets = obExample() // "Hello, Swift!"
  • 132. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String = "Swift") -> String { return "Hello, (who)!" } // call like let greets = obExample(who:"KCDC") // "Hello, KCDC!" let defGreets = obExample() // "Hello, Swift!"
  • 133. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String = "Swift") -> String { return "Hello, (who)!" } // call like let greets = obExample(who:"KCDC") // "Hello, KCDC!" let defGreets = obExample() // "Hello, Swift!"
  • 134. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (who :String = "Swift") -> String { return "Hello, (who)!" } // call like let greets = obExample(who:"KCDC") // "Hello, KCDC!" let defGreets = obExample() // "Hello, Swift!"
  • 135. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", who them :String = "Swift") { print("(what), (them)!") } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
  • 136. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", who them :String = "Swift") { print("(what), (them)!") } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
  • 137. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", who them :String = "Swift") { print("(what), (them)!") } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
  • 138. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", who them :String = "Swift") { print("(what), (them)!") } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
  • 139. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", who them :String = "Swift") { print("(what), (them)!") } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who: "Felicia") // "bye, Felicia!"
  • 140. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 141. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 142. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 143. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 144. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 145. @genehack › Intro to Swift › KCDC 2017 Functions func obExample (what :String = "Hello", _ who :String = "Swift") { print "(what), (who)!" } // call like obExample(what: "bye") // "bye, Swift!" obExample(what: "bye", who:"Felicia") // COMPILE ERROR obExample(what: "bye", "Felicia") // "bye, Felicia!"
  • 146. @genehack › Intro to Swift › KCDC 2017 Functions func variadiacExample (nums: Int...) { // do something with nums // nums is Array[Int] }
  • 147. @genehack › Intro to Swift › KCDC 2017 Functions func variadiacExample (nums: Int...) { // do something with nums // nums is Array[Int] }
  • 149. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] var sorted = numbers.sorted(by:{ (n1: Int, n2: Int) -> Bool in return n2 > n1 }) // sorted = [1, 2, 13, 32, 56, 120]
  • 150. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] var sorted = numbers.sorted(by:{ (n1: Int, n2: Int) -> Bool in return n2 > n1 }) // sorted = [1, 2, 13, 32, 56, 120]
  • 151. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] var sorted = numbers.sorted(by:{ (n1: Int, n2: Int) -> Bool in return n2 > n1 }) // sorted = [1, 2, 13, 32, 56, 120]
  • 152. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] var sorted = numbers.sorted(by:{ (n1: Int, n2: Int) -> Bool in return n2 > n1 }) // sorted = [1, 2, 13, 32, 56, 120] This is already the func sig for sorted(by:)!
  • 153. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // inferred param & return types var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1}) // sorted = [1, 2, 13, 32, 56, 120]
  • 154. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // inferred param & return types var sorted = numbers.sorted(by: {n1, n2 in return n2 > n1}) // sorted = [1, 2, 13, 32, 56, 120]
  • 155. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // positionally named parameters var sorted = numbers.sorted(by: {return $0 > $1}) // sorted = [1, 2, 13, 32, 56, 120]
  • 156. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // positionally named parameters var sorted = numbers.sorted(by: {return $0 > $1}) // sorted = [1, 2, 13, 32, 56, 120]
  • 157. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // when closure is last param, // param name & parens optional var sorted = numbers.sorted { $0 > $1 } // sorted = [1, 2, 13, 32, 56, 120]
  • 158. @genehack › Intro to Swift › KCDC 2017 Closures let numbers = [2,1,56,32,120,13] // when closure is last param, // param name & parens optional var sorted = numbers.sorted { $0 > $1 } // sorted = [1, 2, 13, 32, 56, 120]
  • 159. @genehack › Intro to Swift › KCDC 2017 Closures var sorted = numbers.sorted(by:{ (n1: Int, n2: Int) -> Bool in return n2 > n1 }) var sorted = numbers.sorted { $0 > $1 }
  • 160. OOP is also well supported
  • 161. @genehack › Intro to Swift › KCDC 2017 Classes class Dog { }
  • 162. @genehack › Intro to Swift › KCDC 2017 Properties class Dog { var name: String let noise = "WOOF!" }
  • 163. @genehack › Intro to Swift › KCDC 2017 Properties class Dog { var name: String let noise = "WOOF!" }
  • 164. @genehack › Intro to Swift › KCDC 2017 Properties class Dog { var name: String let noise = "WOOF!" } Class ‘Dog’ has no initializers
  • 165. @genehack › Intro to Swift › KCDC 2017 Properties class Dog { var name: String? let noise = "WOOF!" }
  • 166. @genehack › Intro to Swift › KCDC 2017 Properties class Dog { var name: String? let noise = "WOOF!" }
  • 167. @genehack › Intro to Swift › KCDC 2017 Initializers class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } }
  • 168. @genehack › Intro to Swift › KCDC 2017 Initializers class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } }
  • 169. @genehack › Intro to Swift › KCDC 2017 Initializers class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } }
  • 170. @genehack › Intro to Swift › KCDC 2017 Deinitializers class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } deinit () { // do any cleanup here } }
  • 171. @genehack › Intro to Swift › KCDC 2017 Deinitializers class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } deinit () { // do any cleanup here } }
  • 172. @genehack › Intro to Swift › KCDC 2017 Methods class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } func speak () -> String { return self.noise } }
  • 173. @genehack › Intro to Swift › KCDC 2017 Methods class Dog { var name: String let noise = "WOOF" init (name: String) { self.name = name } func speak () -> String { return self.noise } }
  • 174. @genehack › Intro to Swift › KCDC 2017 Using Objects let sammy = Dog(name: "Sammy"); // sammy is Dog print(sammy.name) // prints "Sammyn" print(sammy.speak()) // prints "WOOF!n" sammy.name = "Buster" // works b/c prop is var
  • 175. @genehack › Intro to Swift › KCDC 2017 Using Objects let sammy = Dog(name: "Sammy"); // sammy is Dog print(sammy.name) // prints "Sammyn" print(sammy.speak()) // prints "WOOF!n" sammy.name = "Buster" // works b/c prop is var
  • 176. @genehack › Intro to Swift › KCDC 2017 Using Objects let sammy = Dog(name: "Sammy"); // sammy is Dog print(sammy.name) // prints "Sammyn" print(sammy.speak()) // prints "WOOF!n" sammy.name = "Buster" // works b/c prop is var
  • 177. @genehack › Intro to Swift › KCDC 2017 Using Objects let sammy = Dog(name: "Sammy"); // sammy is Dog print(sammy.name) // prints "Sammyn" print(sammy.speak()) // prints "WOOF!n" sammy.name = "Buster" // works b/c prop is var
  • 178. @genehack › Intro to Swift › KCDC 2017 Using Objects let sammy = Dog(name: "Sammy"); // sammy is Dog print(sammy.name) // prints "Sammyn" print(sammy.speak()) // prints "WOOF!n" sammy.name = "Buster" // works b/c prop is var
  • 179. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set { // this is horrible, don't do this self.birthYear = currentYear - newValue } } }
  • 180. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set { // this is horrible, don't do this self.birthYear = currentYear - newValue } } }
  • 181. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set { // this is horrible, don't do this self.birthYear = currentYear - newValue } } }
  • 182. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set { // this is horrible, don't do this self.birthYear = currentYear - newValue } } }
  • 183. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set (age) { // this is horrible, don't do this self.birthYear = currentYear - age } } }
  • 184. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set (age) { // this is horrible, don't do this self.birthYear = currentYear - age } } }
  • 185. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { get { return currentYear - self.birthYear } set (age) { // this is horrible, don't do this self.birthYear = currentYear - age } } }
  • 186. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { willSet { // runs before property value changes } didSet { // runs after property value changes } } }
  • 187. @genehack › Intro to Swift › KCDC 2017 Computed Properties class Dog { var age :Int { willSet { // runs before property value changes } didSet { // runs after property value changes } } }
  • 189. @genehack › Intro to Swift › KCDC 2017 Inheritance class Animal { } class Dog : Animal { }
  • 190. @genehack › Intro to Swift › KCDC 2017 Inheritance class Animal { } class Dog : Animal { }
  • 191. @genehack › Intro to Swift › KCDC 2017 Inheritance class Animal { let name: String init (name: name) { self.name = name } } class Dog : Animal { override init (name: name) { super.init(name: name) } }
  • 192. @genehack › Intro to Swift › KCDC 2017 Inheritance class Animal { let name: String init (name: name) { self.name = name } } class Dog : Animal { override init (name: name) { super.init(name: name) } }
  • 193. @genehack › Intro to Swift › KCDC 2017 Inheritance class Animal { let name: String init (name: name) { self.name = name } } class Dog : Animal { override init (name: name) { super.init(name: name) } }
  • 194. @genehack › Intro to Swift › KCDC 2017 Overrides class Animal { func speak() { ... } } class Dog : Animal { override func speak () { ... } }
  • 195. @genehack › Intro to Swift › KCDC 2017 Overrides class Animal { func speak() { ... } } class Dog : Animal { override func speak () { ... } }
  • 197. @genehack › Intro to Swift › KCDC 2017 Structs struct Animal { var name: String var noise: String init (name: String, makes: String) { self.name = name noise = makes } func speak () -> String { return noise } }
  • 198. @genehack › Intro to Swift › KCDC 2017 Structs struct Animal { var name: String var noise: String init (name: String, makes: String) { self.name = name noise = makes } func speak () -> String { return noise } }
  • 199. @genehack › Intro to Swift › KCDC 2017 Structs struct Animal { var name: String var noise: String init (name: String, makes: String) { self.name = name noise = makes } func speak () -> String { return noise } }
  • 200. @genehack › Intro to Swift › KCDC 2017 Structs struct Animal { var name: String var noise: String init (name: String, makes: String) { self.name = name noise = makes } func speak () -> String { return noise } }
  • 201. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest } var conf = OpenSourceConfs.KCDC // conf is type OpenSourceConfs
  • 202. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest } var conf = OpenSourceConfs.KCDC // conf is type OpenSourceConfs
  • 203. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest } var conf = OpenSourceConfs.KCDC // conf is type OpenSourceConfs
  • 204. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest } var conf = OpenSourceConfs.KCDC // conf is type OpenSourceConfs
  • 205. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs :Int { case KCDC = 1 case OpenWest } var conf = OpenSourceConfs.OpenWest // conf is type OpenSourceConfs conf.rawValue // 2
  • 206. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs :Int { case KCDC = 1 case OpenWest } var conf = OpenSourceConfs.OpenWest // conf is type OpenSourceConfs conf.rawValue // 2
  • 207. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs :Int { case KCDC = 1 case OpenWest } var conf = OpenSourceConfs.OpenWest // conf is type OpenSourceConfs conf.rawValue // 2
  • 208. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs :Int { case KCDC = 1 case OpenWest } var conf = OpenSourceConfs.OpenWest // conf is type OpenSourceConfs conf.rawValue // 2
  • 209. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest func describe() -> String { switch self { case .KCDC: return "Hello Kansas City!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC conf.describe()
  • 210. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest func describe() -> String { switch self { case .KCDC: return "Hello Kansas City!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC conf.describe()
  • 211. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest func describe() -> String { switch self { case .KCDC: return "Hello Kansas City!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC conf.describe()
  • 212. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC case OpenWest func describe() -> String { switch self { case .KCDC: return "Hello Kansas City!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC conf.describe()
  • 213. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC(let location): return "Hello (location)!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC("Olathe") conf.describe()
  • 214. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC(let location): return "Hello (location)!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC("Olathe") conf.describe()
  • 215. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC(let location): return "Hello (location)!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC("Olathe") conf.describe()
  • 216. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC(let location): return "Hello (location)!" case .OpenWest: return "Hello Salt Lake City!" } } } var conf = OpenSourceConfs.KCDC("Olathe") conf.describe()
  • 217. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC (let location): return "Hello (location)!" case .OpenWest: return "Hello Salt Lake City!" } } } var kcdc2017 = OpenSourceConfs.KCDC("KC") var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
  • 218. @genehack › Intro to Swift › KCDC 2017 Enumerations enum OpenSourceConfs { case KCDC(String) case OpenWest func describe() -> String { switch self { case .KCDC (let location): return "Hello (location)!" case .OpenWest return "Hello Salt Lake City!" } } } var kcdc2017 = OpenSourceConfs.KCDC("KC") var kcdc2018 = OpenSourceConfs.KCDC("Olathe")
  • 219. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String }
  • 220. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String }
  • 221. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String }
  • 222. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String }
  • 223. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 224. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 225. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 226. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 227. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 228. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 229. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { var noise: String { get } func talk () -> String mutating func mute() } class Dog: Talker { var noise: String init (noise: String) { self.noise = noise } func talk () -> String { return noise } func mute () { noise = "" } }
  • 230. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { ... } class Dog: Talker { ... } class Fish { ... } var sammy: Talker sammy = Dog(noise: "WOOF!") sammy = Fish() // compile error
  • 231. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { ... } class Dog: Talker { ... } class Fish { ... } var sammy: Talker sammy = Dog(noise: "WOOF!") sammy = Fish() // compile error
  • 232. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { ... } class Dog: Talker { ... } class Fish { ... } var sammy: Talker sammy = Dog(noise: "WOOF!") sammy = Fish() // compile error
  • 233. @genehack › Intro to Swift › KCDC 2017 Protocols protocol Talker { ... } class Dog: Talker { ... } class Fish { ... } var sammy: Talker sammy = Dog(noise: "WOOF!") sammy = Fish() // compile error
  • 234. @genehack › Intro to Swift › KCDC 2017 Extensions extension Int { func squared () -> Int { return self * self } } let foo = 2 print(foo.squared()) // 4 print(foo.squared().squared()) // 16
  • 235. @genehack › Intro to Swift › KCDC 2017 Extensions extension Int { func squared () -> Int { return self * self } } let foo = 2 print(foo.squared()) // 4 print(foo.squared().squared()) // 16
  • 236. @genehack › Intro to Swift › KCDC 2017 Extensions extension Int { func squared () -> Int { return self * self } } let foo = 2 print(foo.squared()) // 4 print(foo.squared().squared()) // 16
  • 237. @genehack › Intro to Swift › KCDC 2017 Extensions extension Int { func squared () -> Int { return self * self } } let foo = 2 print(foo.squared()) // 4 print(foo.squared().squared()) // 16
  • 238. Exceptions & Error Handling
  • 239. @genehack › Intro to Swift › KCDC 2017 Exceptions enum TalkErrors: Error { case TooShort case TooLong case TooBoring } func giveATalk (talk: String) throws -> String { if talk == "boring" { throw TalkErrors.TooBoring } return "talk!" }
  • 240. @genehack › Intro to Swift › KCDC 2017 Exceptions enum TalkErrors: Error { case TooShort case TooLong case TooBoring } func giveATalk (talk: String) throws -> String { if talk == "boring" { throw TalkErrors.TooBoring } return "talk!" }
  • 241. @genehack › Intro to Swift › KCDC 2017 Exceptions enum TalkErrors: Error { case TooShort case TooLong case TooBoring } func giveATalk (talk: String) throws -> String { if talk == "boring" { throw TalkErrors.TooBoring } return "talk!" }
  • 242. @genehack › Intro to Swift › KCDC 2017 Exceptions enum TalkErrors: Error { case TooShort case TooLong case TooBoring } func giveATalk (talk: String) throws -> String { if talk == "boring" { throw TalkErrors.TooBoring } return "talk!" }
  • 243. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "boring") print(thisTalk) } catch { print(error) }
  • 244. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "boring") print(thisTalk) } catch { print(error) }
  • 245. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "boring") print(thisTalk) } catch { print(error) }
  • 246. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "boring") print(thisTalk) } catch { print(error) }
  • 247. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "fine") print(thisTalk) } catch TalkErrors.TooLong { print("shut up already") } catch let talkError as TalkErrors { print("Talk error: (talkError).") } catch { print (error) }
  • 248. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "fine") print(thisTalk) } catch TalkErrors.TooLong { print("shut up already") } catch let talkError as TalkErrors { print("Talk error: (talkError).") } catch { print (error) }
  • 249. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "fine") print(thisTalk) } catch TalkErrors.TooLong { print("shut up already") } catch let talkError as TalkErrors { print("Talk error: (talkError).") } catch { print (error) }
  • 250. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "fine") print(thisTalk) } catch TalkErrors.TooLong { print("shut up already") } catch let talkError as TalkErrors { print("Talk error: (talkError).") } catch { print (error) }
  • 251. @genehack › Intro to Swift › KCDC 2017 Exceptions do { let thisTalk = try giveATalk(talk: "fine") print(thisTalk) } catch TalkErrors.TooLong { print("shut up already") } catch let talkError as TalkErrors { print("Talk error: (talkError).") } catch { print (error) }
  • 252. @genehack › Intro to Swift › KCDC 2017 Exceptions // silently discards error let thisTalk = try? giveATalk(talk:"fine") // thisTalk isa String?
  • 253. @genehack › Intro to Swift › KCDC 2017 Exceptions // silently discards error let thisTalk = try? giveATalk(talk:"fine") // thisTalk isa String?
  • 254. @genehack › Intro to Swift › KCDC 2017 Exceptions // silently discards error let thisTalk = try? giveATalk(talk:"fine") // thisTalk isa String?
  • 255. @genehack › Intro to Swift › KCDC 2017 Defer func needsMuchSetupAndTearDown () { // do the setup here, open files, etc. defer { // and do the cleanup here, // right next to set up } // other code here. }
  • 256. @genehack › Intro to Swift › KCDC 2017 Defer func needsMuchSetupAndTearDown () { // do the setup here, open files, etc. defer { // and do the cleanup here, // right next to set up } // other code here. }
  • 260.
  • 261.
  • 263.
  • 265.
  • 266. GIMME YR TALKS CFP OPEN NOW! Oct 6-7 seagl.org