A Modest Introduction to Swift

285
A Modest Introduction To Swift OpenWest 2016 15 July 2016 John SJ Anderson @genehack

Transcript of A Modest Introduction to Swift

Page 1: A Modest Introduction to Swift

A Modest Introduction

To SwiftOpenWest 201615 July 2016

John SJ Anderson@genehack

Page 2: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Sorry!

These are my obligatory two "Swift" jokes, wanted to get those out of the way...

Page 3: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Hi I'm John

• VP Tech, Infinity Interactive • Ex-biologist • Linux • Perl tribe • Polyglot coder

• Just this guy, you know?

Page 4: A Modest Introduction to Swift

Polyglotism

Page 5: A Modest Introduction to Swift

Disclaimer

Page 6: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Swift?

So, what is Swift?

Page 7: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Introduced in 2014

Page 8: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Went Open Source at version 2.2

Page 9: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Version 3 just released

Page 10: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Version 3 just released("technology preview")

this talk is specifically about 2.2

Page 11: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Originally MacOS only

Page 12: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Now on Linux too.

Page 13: A Modest Introduction to Swift
Page 14: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Android in the works!

Page 15: A Modest Introduction to Swift
Page 16: A Modest Introduction to Swift
Page 17: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Android in the works!DONE!!

Page 18: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Windows too?

Page 19: A Modest Introduction to Swift

https://swiftforwindows.codeplex.com/

Page 20: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Originally targeted MacOS, iOS,

watchOS, and tvOS

Page 21: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

With expanded platform support, offers potential "single-language stack" advantages a la Node

Page 22: A Modest Introduction to Swift

So what's it like, man?

Page 23: A Modest Introduction to Swift

First, a brief digression…

Page 24: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

How many MacOS / iOS users do we have here?

Page 25: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

How many MacOS / iOS developers do we have?

Page 26: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

The dirty little secret of developing for Apple

Page 27: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

From In The Beginning Was The Command Line

by Neal Stephenson

During the late 1980's and early 1990's I spent a lot of time programming Macintoshes, and eventually decided for fork over several hundred dollars for an Apple product called the Macintosh Programmer's Workshop, or MPW. MPW had competitors, but it was unquestionably the premier software development system for the Mac. It was what Apple's own engineers used to write Macintosh code. Given that MacOS was far more technologically advanced, at the time, than its competition, and that Linux did not even exist yet, and given that this was the actual program used by Apple's world-class team of creative engineers, I had high expectations. It arrived on a stack of floppy disks about a foot high, and so there was plenty of time for my excitement to build during the endless installation process. The first time I launched MPW, I was probably expecting some kind of touch-feely multimedia showcase. Instead it was austere, almost to the point of being intimidating.

Page 28: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Weird Pascal-based naming and calling

conventions

Page 29: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

HANDLES?!?

Page 30: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ObjectiveC's "syntax"

Page 31: A Modest Introduction to Swift

Swift is the Mac of Apple programming languages

Page 32: A Modest Introduction to Swift

Swift Syntax

Page 33: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Comments//thisisacomment

Page 34: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Comments/*thisisamulti-linecomment*/

Page 35: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Comments/*thisisa/*_nested_multi-linecomment,*/whichiscool!*/

Page 36: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Comments///doccomment

Page 37: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesvarfoo=1varbar:Intvarbaz="whee!"

Page 38: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesvarfoo=1varbar:Intvarbaz="whee!"

Page 39: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesvarfoo=1varbar:Intvarbaz="whee!"

Page 40: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesvarfoo=1varbar:Intvarbaz="whee!"

Page 41: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variables

letbar=1bar+=1//^^compiletimeerror!

Page 42: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variables

letbar=1bar+=1//^^compiletimeerror!

Page 43: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesletbar

//alsoacompiletimeerror

/*YoucanNOThaveanuninitializedanduntypedvariable.Youalsocan'tuseaninitializedvariable_atall_*/

Page 44: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Variablesletbar

//alsoacompiletimeerror

/*YoucanNOThaveanuninitializedanduntypedvariable.Youalsocan'tuseaninitializedvariable_atall_*/

Page 45: A Modest Introduction to Swift

How friggin' awesome is that?

Page 46: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Operators

Page 47: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletn=1

ifn>1{print("wegotabigNhere")}

Page 48: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletn=1

ifn>1{print("wegotabigNhere")}

Page 49: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletn=1

ifn>1{print("wegotabigNhere")}

Page 50: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletarr=[1,2,3,4]varsum=0

foreleminarr{sum+=elem}

//sumnowis10

Page 51: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletarr=[1,2,3,4]varsum=0

foreleminarr{sum+=elem}

//sumnowis10

Page 52: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletarr=[1,2,3,4]varsum=0

foreleminarr{sum+=elem}

//sumnowis10

Page 53: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletarr=[1,2,3,4]varsum=0

foreleminarr{sum+=elem}

//sumnowis10

Page 54: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlforindexin1...10{#dosomething10times}

Page 55: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlforindexin1...10{#dosomething10times}

Page 56: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlforindexin1..<10{#dosomething9times}

Page 57: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlforindexin1..<10{#dosomething9times}

Page 58: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow ControlvarcountDown=5whilecountDown>0{countDown--}

Page 59: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow ControlvarcountDown=5whilecountDown>0{countDown--}

Page 60: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow ControlvarcountUp=0repeat{countUp++}whilecountUp<5

Page 61: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=2

switchsample{case0:print("Is0")

case2:print("Is2")

default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}

Page 62: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=2

switchsample{case0:print("Is0")

case2:print("Is2")

default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}

Page 63: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=2

switchsample{case0:print("Is0")

case2:print("Is2")

default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}

Page 64: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=2

switchsample{case0:print("Is0")

case2:print("Is2")

default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}

No fallthru by default!

Page 65: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=2

switchsample{case0:print("Is0")

case2:print("Is2")

default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}

Page 66: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample="foo"

switchsample{case"foo":print("Isfoo")

case"bar":print("Isbar")

default://mandatorywhencasesnotexclusiveprint("Notfooorbar,isit.")}

Page 67: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample="foo"

switchsample{case"foo":print("Isfoo")

case"bar":print("Isbar")

default://mandatorywhencasesnotexclusiveprint("Notfooorbar,isit.")}

Page 68: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo",2):print("Isfoo,2")

case("bar",_):print("Isbar")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 69: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo",2):print("Isfoo,2")

case("bar",_):print("Isbar")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 70: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo",2):print("Isfoo,2")

case("bar",_):print("Isbar")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 71: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo",2):print("Isfoo,2")

case("bar",_):print("Isbar")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 72: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo","bar"):print("Isfoo,bar")

case(letone,lettwo):print("Is\(one)and\(two)")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 73: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Flow Controlletsample=("foo",2)

switchsample{case("foo","bar"):print("Isfoo,bar")

case(letone,lettwo):print("Is\(one)and\(two)")

default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}

Page 74: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StringsvarmyString="thisisastring"

ifmyString.isEmpty{//dosomething}

myString+="andthisisalongerstring"

Page 75: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StringsvarmyString="thisisastring"

ifmyString.isEmpty{//dosomething}

myString+="andthisisalongerstring"

Page 76: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StringsvarmyString="thisisastring"

ifmyString.isEmpty{//dosomething}

myString+="andthisisalongerstring"

Page 77: A Modest Introduction to Swift

Swift is very strongly typed.

Page 78: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Typingvarfoo=1//fooisanInt

varbar:Int//barisanuninit'dInt

varbaz=Int()

ifbazisInt{print("NiceIntyougotthere")}

Page 79: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Typingvarfoo=1//fooisanInt

varbar:Int//barisanuninit'dInt

varbaz=Int()

ifbazisInt{print("NiceIntyougotthere")}

Page 80: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Typingvarfoo=1//fooisanInt

varbar:Int//barisanuninit'dInt

varbaz=Int()

ifbazisInt{print("NiceIntyougotthere")}

Page 81: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Typingvarfoo=1//fooisanInt

varbar:Int//barisanuninit'dInt

varbaz=Int()

ifbazisInt{print("NiceIntyougotthere")}

Page 82: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Typingvarfoo=1//fooisanInt

varbar:Int//barisanuninit'dInt

varbaz=Int()

ifbazisInt{print("NiceIntyougotthere")}

Page 83: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Castsvarfoo=1//fooisanInt

varbar=String(foo)//"1"

varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString

varforceBaz=stringishThingas!String

Page 84: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Castsvarfoo=1//fooisanInt

varbar=String(foo)//"1"

varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString

varforceBaz=stringishThingas!String

Page 85: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Castsvarfoo=1//fooisanInt

varbar=String(foo)//"1"

varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString

varforceBaz=stringishThingas!String

Page 86: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Castsvarfoo=1//fooisanInt

varbar=String(foo)//"1"

varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString

varforceBaz=stringishThingas!String

Page 87: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//Whenavariablemaynothaveavaluevarbar:Int?

//testifbar!=nil{//hasavalue}

Page 88: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//Whenavariablemaynothaveavaluevarbar:Int?

//testifbar!=nil{//hasavalue}

Page 89: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//Whenavariablemaynothaveavaluevarbar:Int?

//testifbar!=nil{//hasavalue}

Page 90: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}

//unwrappingnil-->runtimeexception!

Page 91: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}

//unwrappingnil-->runtimeexception!

Page 92: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}

//unwrappingnil-->runtimeexception!

Page 93: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-letvarbar:Int?

ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}

Page 94: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-letvarbar:Int?

ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}

Page 95: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-letvarbar:Int?

ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}

Page 96: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-varvarbar:Int?

ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}

Page 97: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-varvarbar:Int?

ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}

Page 98: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

if-varvarbar:Int?

ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}

Page 99: A Modest Introduction to Swift

Types of Values

Page 100: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Tupleslettuple=("foo",42)

letfirst=tuple.0//"foo"

letlabeledTuple=(one:"foo",two:42)

letsecond=labeledTuple.two//42

Page 101: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Tupleslettuple=("foo",42)

letfirst=tuple.0//"foo"

letlabeledTuple=(one:"foo",two:42)

letsecond=labeledTuple.two//42

Page 102: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Tupleslettuple=("foo",42)

letfirst=tuple.0//"foo"

letlabeledTuple=(one:"foo",two:42)

letsecond=labeledTuple.two//42

Page 103: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Tupleslettuple=("foo",42)

letfirst=tuple.0//"foo"

letlabeledTuple=(one:"foo",two:42)

letsecond=labeledTuple.two//42

Page 104: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Tupleslettuple=("foo",42)

letfirst=tuple.0//"foo"

letlabeledTuple=(one:"foo",two:42)

letsecond=labeledTuple.two//42

Page 105: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Arraysletnums=[1,2,3]

varstrs:[String]

//_can_mix&matchletmixed=[1,"foo"]

//butyouprobablyshouldn't

Page 106: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Arraysletnums=[1,2,3]

varstrs:[String]

//_can_mix&matchletmixed=[1,"foo"]

//butyouprobablyshouldn't

Page 107: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Arraysletnums=[1,2,3]

varstrs:[String]

//_can_mix&matchletmixed=[1,"foo"]

//butyouprobablyshouldn't

Page 108: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Arraysletnums=[1,2,3]

varstrs:[String]

//_can_mix&matchletmixed=[1,"foo"]

//butyouprobablyshouldn't

Page 109: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Arraysletnums=[1,2,3]

varstrs:[String]

//_can_mix&matchletmixed=[1,"foo"]

//butyouprobablyshouldn't

Page 110: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]

//capitalCityStateshastype//[String:String]

Page 111: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]

//capitalCityStateshastype//[String:String]

Page 112: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]

//capitalCityStateshastype//[String:String]

Page 113: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]

//capitalCityStateshastype//[String:NSObject]

Page 114: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]

//capitalCityStateshastype//[String:NSObject]

Page 115: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]

//capitalCityStateshastype//[String:NSObject]

Page 116: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]

//capitalCityStateshastype//[String:NSObject]

Page 117: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

SetsvarpetSet:Set=["cat","dog","fish","dog"]

petSet.count//returns3

Page 118: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

SetsvarpetSet:Set=["cat","dog","fish","dog"]

petSet.count//returns3

Page 119: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

SetsvarpetSet:Set=["cat","dog","fish","dog"]

petSet.count//returns3

Page 120: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

SetsvarpetSet:Set=["cat","dog","fish","dog"]

petSet.count//returns3

Page 121: A Modest Introduction to Swift

Functions

Page 122: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(){print("Hello,Salem!")}

//calllike:obExample()

Page 123: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String){print("Hello,\(who)!")}

//calllikeobExample("OpenWest")

Page 124: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String){print("Hello,\(who)!")}

//calllikeobExample("OpenWest")

Page 125: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String){print("Hello,\(who)!")}

//calllikeobExample("OpenWest")

Page 126: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String)->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("OpenWest")

//greets=="Hello,OpenWest"

Page 127: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String)->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("OpenWest")

//greets=="Hello,OpenWest"

Page 128: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"

letgreets=obExample()//"Hello,Salem!"

Page 129: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"

letgreets=obExample()//"Hello,Salem!"

Page 130: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"

letgreets=obExample()//"Hello,Salem!"

Page 131: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}

//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"

letgreets=obExample()//"Hello,Salem!"

Page 132: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 133: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 134: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 135: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 136: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 137: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 138: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 139: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 140: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample()//"Hello,Salem!"obExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//"bye,felicia"obExample("bye","Salem")//COMPILEERRORobExample(what:"bye",who:"Salem")//COMPILEERRORobExample(who:"Salem",what:"bye")//COMPILEERROR

Page 141: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",whothem:String="Salem"){print"\(what),\(them)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",them:"Felicia")//"bye,Felicia!"

Page 142: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",whothem:String="Salem"){print"\(what),\(them)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",them:"Felicia")//"bye,Felicia!"

Page 143: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",whothem:String="Salem"){print"\(what),\(them)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",them:"Felicia")//"bye,Felicia!"

Page 144: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",whothem:String="Salem"){print"\(what),\(them)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",them:"Felicia")//"bye,Felicia!"

Page 145: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",whothem:String="Salem"){print"\(what),\(them)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",them:"Felicia")//"bye,Felicia!"

Page 146: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 147: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 148: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 149: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 150: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 151: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncobExample(what:String="Hello",_who:String="Salem"){print"\(what),\(who)!"}

//calllikeobExample("bye")//"bye,Salem!"obExample("bye",who:"Felicia")//COMPILEERRORobExample("bye","Felicia")//"bye,Felicia!"

Page 152: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncvariadiacExample(nums:Int...){//dosomethingwithnums//numsisArray[Int]}

Page 153: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

FunctionsfuncvariadiacExample(nums:Int...){//dosomethingwithnums//numsisArray[Int]}

Page 154: A Modest Introduction to Swift

Functions are first-class citizens

Page 155: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

varsorted=numbers.sort({(n1:Int,n2:Int)->Boolinreturnn2>n1})

//sorted=[1,2,13,32,56,120]

Page 156: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

varsorted=numbers.sort({(n1:Int,n2:Int)->Boolinreturnn2>n1})

//sorted=[1,2,13,32,56,120]

Page 157: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

varsorted=numbers.sort({(n1:Int,n2:Int)->Boolinreturnn2>n1})

//sorted=[1,2,13,32,56,120]

Page 158: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

varsorted=numbers.sort({(n1:Int,n2:Int)->Boolinreturnn2>n1})

//sorted=[1,2,13,32,56,120]

This is already the func sig for sort!

Page 159: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//inferredparam&returntypesvarsorted=numbers.sort({n1,n2inreturnn2>n1})

//sorted=[1,2,13,32,56,120]

Page 160: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//inferredparam&returntypesvarsorted=numbers.sort({n1,n2inreturnn2>n1})

//sorted=[1,2,13,32,56,120]

Page 161: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//positionallynamedparametersvarsorted=numbers.sort({return$0>$1})

//sorted=[1,2,13,32,56,120]

Page 162: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//positionallynamedparametersvarsorted=numbers.sort({return$0>$1})

//sorted=[1,2,13,32,56,120]

Page 163: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//whenclosureislastparam,parensoptionalvarsorted=numbers.sort{$0>$1}

//sorted=[1,2,13,32,56,120]

Page 164: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Closuresletnumbers=[2,1,56,32,120,13]

//whenclosureislastparam,parensoptionalvarsorted=numbers.sort{$0>$1}

//sorted=[1,2,13,32,56,120]

Page 165: A Modest Introduction to Swift

OOP is also well supported

Page 166: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ClassesclassDog{

}

Page 167: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

PropertiesclassDog{varname:Stringletnoise="WOOF!"}

just variable declarations inside the class

Page 168: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

PropertiesclassDog{varname:Stringletnoise="WOOF!"}

Page 169: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

PropertiesclassDog{varname:Stringletnoise="WOOF!"}

Class 'Dog' has no initializers

...because you're not allowed to have uninitialized properties.

Page 170: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

PropertiesclassDog{varname:String?letnoise="WOOF!"}

you can fix that by making the property an optional type

Page 171: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

PropertiesclassDog{varname:String?letnoise="WOOF!"}

you can fix that by making the property an optional type

Page 172: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InitializersclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}}

or by providing an initializer that assigns a value to the property

Page 173: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InitializersclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}}

or by providing an initializer that assigns a value to the property

Page 174: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InitializersclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}}

or by providing an initializer that assigns a value to the property

Page 175: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DeinitializersclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}

deinit(){//doanycleanuphere}}

or by providing an initializer that assigns a value to the property

Page 176: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DeinitializersclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}

deinit(){//doanycleanuphere}}

or by providing an initializer that assigns a value to the property

Page 177: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

MethodsclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}

funcspeak()->String{returnself.noise}}

Page 178: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

MethodsclassDog{varname:Stringletnoise="WOOF"

init(name:String){self.name=name}

funcspeak()->String{returnself.noise}}

Page 179: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Using Objectsletsammy=Dog(name:"Sammy");

//sammyisDog

print(sammy.name)//prints"Sammy\n"

print(sammy.speak())//prints"WOOF!\n"

sammy.name="Buster"//worksb/cpropisvar

Page 180: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Using Objectsletsammy=Dog(name:"Sammy");

//sammyisDog

print(sammy.name)//prints"Sammy\n"

print(sammy.speak())//prints"WOOF!\n"

sammy.name="Buster"//worksb/cpropisvar

Page 181: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Using Objectsletsammy=Dog(name:"Sammy");

//sammyisDog

print(sammy.name)//prints"Sammy\n"

print(sammy.speak())//prints"WOOF!\n"

sammy.name="Buster"//worksb/cpropisvar

Page 182: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Using Objectsletsammy=Dog(name:"Sammy");

//sammyisDog

print(sammy.name)//prints"Sammy\n"

print(sammy.speak())//prints"WOOF!\n"

sammy.name="Buster"//worksb/cpropisvar

Page 183: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Using Objectsletsammy=Dog(name:"Sammy");

//sammyisDog

print(sammy.name)//prints"Sammy\n"

print(sammy.speak())//prints"WOOF!\n"

sammy.name="Buster"//worksb/cpropisvar

Page 184: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}

Page 185: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}

Page 186: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}

Page 187: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}

Page 188: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set(age){//thisishorrible,don'tdothisself.birthYear=currentYear-age}}}

Page 189: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set(age){//thisishorrible,don'tdothisself.birthYear=currentYear-age}}}

Page 190: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}

Page 191: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{willSet{//runsbeforepropertyvaluechanges}didSet{//runsafterpropertyvaluechanges}}}

Page 192: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Computed PropertiesclassDog{varage:Int{willSet{//runsbeforepropertyvaluechanges}didSet{//runsafterpropertyvaluechanges}}}

Page 193: A Modest Introduction to Swift

Inheritance

Page 194: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InheritanceclassAnimal{

}

classDog:Animal{

}

Page 195: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InheritanceclassAnimal{

}

classDog:Animal{

}

Page 196: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InheritanceclassAnimal{letname:String

init(name:name){self.name=name}}

classDog:Animal{init(name:name){super.init(name:name)}}

Page 197: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

InheritanceclassAnimal{letname:String

init(name:name){self.name=name}}

classDog:Animal{init(name:name){super.init(name:name)}}

Page 198: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

OverridesclassAnimal{funcspeak(){...}}

classDog:Animal{overridefuncspeak(){...}}

Page 199: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

OverridesclassAnimal{funcspeak(){...}}

classDog:Animal{overridefuncspeak(){...}}

Page 200: A Modest Introduction to Swift

Structs & Enumerations

Page 201: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StructsstructAnimal{varname:Stringvarnoise:String

init(name:String,makes:String){self.name=namenoise=makes}

funcspeak()->String{returnnoise}}

very much like classes -- support properties, initializers, methods, etc.

big difference: structs are pass-by-value; classes/objects are pass-by-reference

Page 202: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StructsstructAnimal{varname:Stringvarnoise:String

init(name:String,makes:String){self.name=namenoise=makes}

funcspeak()->String{returnnoise}}

very much like classes -- support properties, initializers, methods, etc.

big difference: structs are pass-by-value; classes/objects are pass-by-reference

Page 203: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StructsstructAnimal{varname:Stringvarnoise:String

init(name:String,makes:String){self.name=namenoise=makes}

funcspeak()->String{returnnoise}}

very much like classes -- support properties, initializers, methods, etc.

big difference: structs are pass-by-value; classes/objects are pass-by-reference

Page 204: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

StructsstructAnimal{varname:Stringvarnoise:String

init(name:String,makes:String){self.name=namenoise=makes}

funcspeak()->String{returnnoise}}

very much like classes -- support properties, initializers, methods, etc.

big difference: structs are pass-by-value; classes/objects are pass-by-reference

Page 205: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}

varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs

enums provide a constrained set of enumerated values

Page 206: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}

varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs

enums provide a constrained set of enumerated values

Page 207: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}

varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs

enums provide a constrained set of enumerated values

Page 208: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}

varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs

enums provide a constrained set of enumerated values

Page 209: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}

varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs

conf.rawValue//2

enums provide a constrained set of enumerated values

Page 210: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}

varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs

conf.rawValue//2

enums provide a constrained set of enumerated values

Page 211: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}

varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs

conf.rawValue//2

enums provide a constrained set of enumerated values

Page 212: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}

varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs

conf.rawValue//2

enums provide a constrained set of enumerated values

Page 213: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPCreturn"ithoughtperlwasdead"}}

}

varconf=OpenSourceConfs.OpenWestconf.describe()

enums provide a constrained set of enumerated values

Page 214: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPCreturn"ithoughtperlwasdead"}}

}

varconf=OpenSourceConfs.OpenWestconf.describe()

enums provide a constrained set of enumerated values

Page 215: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPCreturn"ithoughtperlwasdead"}}

}

varconf=OpenSourceConfs.OpenWestconf.describe()

enums provide a constrained set of enumerated values

Page 216: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPCreturn"ithoughtperlwasdead"}}

}

varconf=OpenSourceConfs.OpenWestconf.describe()

enums provide a constrained set of enumerated values

Page 217: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")yapc2016.describe()

enums provide a constrained set of enumerated values

Page 218: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")yapc2016.describe()

enums provide a constrained set of enumerated values

Page 219: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")yapc2016.describe()

enums provide a constrained set of enumerated values

Page 220: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")yapc2016.describe()

enums provide a constrained set of enumerated values

Page 221: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")varyapc2015=OpenSourceConfs.YAPC("SaltLakeCity")

enums provide a constrained set of enumerated values

Page 222: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC(String)

funcdescribe()->String{switchself{case.OpenWest:return"helloutah!"case.YAPC(location)return"thisyearYAPCisin\(location)"}}

}

varyapc2016=OpenSourceConfs.YAPC("Orlando")varyapc2015=OpenSourceConfs.YAPC("SaltLakeCity")

enums provide a constrained set of enumerated values

Page 223: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}

Page 224: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}

Page 225: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}

Page 226: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}

Page 227: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 228: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 229: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 230: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 231: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 232: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 233: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{varnoise:String{get}functalk()->Stringmutatingfuncmute()}

classDog:Talker{varnoise:Stringinit(noise:noise){self.noise=noise}functalk()->String{returnnoise}funcmute(){noise=""}}

Page 234: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{...}

classDog:Talker{...}

classFish{...}

varsammy:Talker

sammy=Dog(noise:"WOOF!")

sammy=Fish()//compileerror

protocols can be used as types in variable declarations

Page 235: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{...}

classDog:Talker{...}

classFish{...}

varsammy:Talker

sammy=Dog(noise:"WOOF!")

sammy=Fish()//compileerror

protocols can be used as types in variable declarations

Page 236: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{...}

classDog:Talker{...}

classFish{...}

varsammy:Talker

sammy=Dog(noise:"WOOF!")

sammy=Fish()//compileerror

protocols can be used as types in variable declarations

Page 237: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ProtocolsprotocolTalker{...}

classDog:Talker{...}

classFish{...}

varsammy:Talker

sammy=Dog(noise:"WOOF!")

sammy=Fish()//compileerror

protocols can be used as types in variable declarations

Page 238: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExtensionsextensionInt{funcsquared()->Int{returnself*self}}

letfoo=2

print(foo.squared())//4print(foo.squared().squared())//16

extensions can be used to extend built-in types to allow them to conform to protocols

Page 239: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExtensionsextensionInt{funcsquared()->Int{returnself*self}}

letfoo=2

print(foo.squared())//4print(foo.squared().squared())//16

extensions can be used to extend built-in types to allow them to conform to protocols

Page 240: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExtensionsextensionInt{funcsquared()->Int{returnself*self}}

letfoo=2

print(foo.squared())//4print(foo.squared().squared())//16

extensions can be used to extend built-in types to allow them to conform to protocols

Page 241: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExtensionsextensionInt{funcsquared()->Int{returnself*self}}

letfoo=2

print(foo.squared())//4print(foo.squared().squared())//16

extensions can be used to extend built-in types to allow them to conform to protocols

Page 242: A Modest Introduction to Swift

Exceptions & Error Handling

Page 243: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}

funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}

Page 244: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}

funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}

Page 245: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}

funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}

Page 246: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}

funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}

Page 247: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}

Page 248: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}

Page 249: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}

Page 250: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}

Page 251: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}

Page 252: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}

Page 253: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}

Page 254: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}

Page 255: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}

Page 256: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()

//thisTalkisaString?

Page 257: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()

//thisTalkisaString?

Page 258: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()

//thisTalkisString?

Page 259: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}

//othercodehere.}

Page 260: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}

//othercodehere.}

Page 261: A Modest Introduction to Swift

Intro to Swift › OpenWest 20160715

DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}

//othercodehere.}

Page 262: A Modest Introduction to Swift

Workspaces

Page 263: A Modest Introduction to Swift

Ill-advisedlive

demotime!

Page 264: A Modest Introduction to Swift

Ill-advisedlive

demotime!

Page 265: A Modest Introduction to Swift
Page 266: A Modest Introduction to Swift
Page 267: A Modest Introduction to Swift
Page 268: A Modest Introduction to Swift
Page 269: A Modest Introduction to Swift
Page 270: A Modest Introduction to Swift
Page 271: A Modest Introduction to Swift
Page 272: A Modest Introduction to Swift
Page 273: A Modest Introduction to Swift
Page 274: A Modest Introduction to Swift
Page 275: A Modest Introduction to Swift
Page 276: A Modest Introduction to Swift

˜

Page 277: A Modest Introduction to Swift
Page 278: A Modest Introduction to Swift

˜ ˜

Page 279: A Modest Introduction to Swift
Page 280: A Modest Introduction to Swift

https://developer.apple.com/swift

Page 281: A Modest Introduction to Swift
Page 282: A Modest Introduction to Swift

This part is a lie.

Page 283: A Modest Introduction to Swift

THANKS!

Page 284: A Modest Introduction to Swift
Page 285: A Modest Introduction to Swift

questions?