A Modest Introduction to Swift
-
Upload
john-anderson -
Category
Internet
-
view
222 -
download
2
Transcript of A Modest Introduction to Swift
A Modest Introduction
To SwiftOpenWest 201615 July 2016
John SJ Anderson@genehack
Intro to Swift › OpenWest 20160715
Sorry!
These are my obligatory two "Swift" jokes, wanted to get those out of the way...
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?
Polyglotism
Disclaimer
Intro to Swift › OpenWest 20160715
Swift?
So, what is Swift?
Intro to Swift › OpenWest 20160715
Introduced in 2014
Intro to Swift › OpenWest 20160715
Went Open Source at version 2.2
Intro to Swift › OpenWest 20160715
Version 3 just released
Intro to Swift › OpenWest 20160715
Version 3 just released("technology preview")
this talk is specifically about 2.2
Intro to Swift › OpenWest 20160715
Originally MacOS only
Intro to Swift › OpenWest 20160715
Now on Linux too.
Intro to Swift › OpenWest 20160715
Android in the works!
Intro to Swift › OpenWest 20160715
Android in the works!DONE!!
Intro to Swift › OpenWest 20160715
Windows too?
https://swiftforwindows.codeplex.com/
Intro to Swift › OpenWest 20160715
Originally targeted MacOS, iOS,
watchOS, and tvOS
Intro to Swift › OpenWest 20160715
With expanded platform support, offers potential "single-language stack" advantages a la Node
So what's it like, man?
First, a brief digression…
Intro to Swift › OpenWest 20160715
How many MacOS / iOS users do we have here?
Intro to Swift › OpenWest 20160715
How many MacOS / iOS developers do we have?
Intro to Swift › OpenWest 20160715
The dirty little secret of developing for Apple
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.
Intro to Swift › OpenWest 20160715
Weird Pascal-based naming and calling
conventions
Intro to Swift › OpenWest 20160715
HANDLES?!?
Intro to Swift › OpenWest 20160715
ObjectiveC's "syntax"
Swift is the Mac of Apple programming languages
Swift Syntax
Intro to Swift › OpenWest 20160715
Comments//thisisacomment
Intro to Swift › OpenWest 20160715
Comments/*thisisamulti-linecomment*/
Intro to Swift › OpenWest 20160715
Comments/*thisisa/*_nested_multi-linecomment,*/whichiscool!*/
Intro to Swift › OpenWest 20160715
Comments///doccomment
Intro to Swift › OpenWest 20160715
Variablesvarfoo=1varbar:Intvarbaz="whee!"
Intro to Swift › OpenWest 20160715
Variablesvarfoo=1varbar:Intvarbaz="whee!"
Intro to Swift › OpenWest 20160715
Variablesvarfoo=1varbar:Intvarbaz="whee!"
Intro to Swift › OpenWest 20160715
Variablesvarfoo=1varbar:Intvarbaz="whee!"
Intro to Swift › OpenWest 20160715
Variables
letbar=1bar+=1//^^compiletimeerror!
Intro to Swift › OpenWest 20160715
Variables
letbar=1bar+=1//^^compiletimeerror!
Intro to Swift › OpenWest 20160715
Variablesletbar
//alsoacompiletimeerror
/*YoucanNOThaveanuninitializedanduntypedvariable.Youalsocan'tuseaninitializedvariable_atall_*/
Intro to Swift › OpenWest 20160715
Variablesletbar
//alsoacompiletimeerror
/*YoucanNOThaveanuninitializedanduntypedvariable.Youalsocan'tuseaninitializedvariable_atall_*/
How friggin' awesome is that?
Intro to Swift › OpenWest 20160715
Operators
Intro to Swift › OpenWest 20160715
Flow Controlletn=1
ifn>1{print("wegotabigNhere")}
Intro to Swift › OpenWest 20160715
Flow Controlletn=1
ifn>1{print("wegotabigNhere")}
Intro to Swift › OpenWest 20160715
Flow Controlletn=1
ifn>1{print("wegotabigNhere")}
Intro to Swift › OpenWest 20160715
Flow Controlletarr=[1,2,3,4]varsum=0
foreleminarr{sum+=elem}
//sumnowis10
Intro to Swift › OpenWest 20160715
Flow Controlletarr=[1,2,3,4]varsum=0
foreleminarr{sum+=elem}
//sumnowis10
Intro to Swift › OpenWest 20160715
Flow Controlletarr=[1,2,3,4]varsum=0
foreleminarr{sum+=elem}
//sumnowis10
Intro to Swift › OpenWest 20160715
Flow Controlletarr=[1,2,3,4]varsum=0
foreleminarr{sum+=elem}
//sumnowis10
Intro to Swift › OpenWest 20160715
Flow Controlforindexin1...10{#dosomething10times}
Intro to Swift › OpenWest 20160715
Flow Controlforindexin1...10{#dosomething10times}
Intro to Swift › OpenWest 20160715
Flow Controlforindexin1..<10{#dosomething9times}
Intro to Swift › OpenWest 20160715
Flow Controlforindexin1..<10{#dosomething9times}
Intro to Swift › OpenWest 20160715
Flow ControlvarcountDown=5whilecountDown>0{countDown--}
Intro to Swift › OpenWest 20160715
Flow ControlvarcountDown=5whilecountDown>0{countDown--}
Intro to Swift › OpenWest 20160715
Flow ControlvarcountUp=0repeat{countUp++}whilecountUp<5
Intro to Swift › OpenWest 20160715
Flow Controlletsample=2
switchsample{case0:print("Is0")
case2:print("Is2")
default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=2
switchsample{case0:print("Is0")
case2:print("Is2")
default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=2
switchsample{case0:print("Is0")
case2:print("Is2")
default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=2
switchsample{case0:print("Is0")
case2:print("Is2")
default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}
No fallthru by default!
Intro to Swift › OpenWest 20160715
Flow Controlletsample=2
switchsample{case0:print("Is0")
case2:print("Is2")
default://mandatorywhencasesnotexclusiveprint("Not0or2,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample="foo"
switchsample{case"foo":print("Isfoo")
case"bar":print("Isbar")
default://mandatorywhencasesnotexclusiveprint("Notfooorbar,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample="foo"
switchsample{case"foo":print("Isfoo")
case"bar":print("Isbar")
default://mandatorywhencasesnotexclusiveprint("Notfooorbar,isit.")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=("foo",2)
switchsample{case("foo",2):print("Isfoo,2")
case("bar",_):print("Isbar")
default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=("foo",2)
switchsample{case("foo",2):print("Isfoo,2")
case("bar",_):print("Isbar")
default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=("foo",2)
switchsample{case("foo",2):print("Isfoo,2")
case("bar",_):print("Isbar")
default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}
Intro to Swift › OpenWest 20160715
Flow Controlletsample=("foo",2)
switchsample{case("foo",2):print("Isfoo,2")
case("bar",_):print("Isbar")
default://mandatorywhencasesnotexclusiveprint("¯\_( )_/¯")}
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("¯\_( )_/¯")}
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("¯\_( )_/¯")}
Intro to Swift › OpenWest 20160715
StringsvarmyString="thisisastring"
ifmyString.isEmpty{//dosomething}
myString+="andthisisalongerstring"
Intro to Swift › OpenWest 20160715
StringsvarmyString="thisisastring"
ifmyString.isEmpty{//dosomething}
myString+="andthisisalongerstring"
Intro to Swift › OpenWest 20160715
StringsvarmyString="thisisastring"
ifmyString.isEmpty{//dosomething}
myString+="andthisisalongerstring"
Swift is very strongly typed.
Intro to Swift › OpenWest 20160715
Typingvarfoo=1//fooisanInt
varbar:Int//barisanuninit'dInt
varbaz=Int()
ifbazisInt{print("NiceIntyougotthere")}
Intro to Swift › OpenWest 20160715
Typingvarfoo=1//fooisanInt
varbar:Int//barisanuninit'dInt
varbaz=Int()
ifbazisInt{print("NiceIntyougotthere")}
Intro to Swift › OpenWest 20160715
Typingvarfoo=1//fooisanInt
varbar:Int//barisanuninit'dInt
varbaz=Int()
ifbazisInt{print("NiceIntyougotthere")}
Intro to Swift › OpenWest 20160715
Typingvarfoo=1//fooisanInt
varbar:Int//barisanuninit'dInt
varbaz=Int()
ifbazisInt{print("NiceIntyougotthere")}
Intro to Swift › OpenWest 20160715
Typingvarfoo=1//fooisanInt
varbar:Int//barisanuninit'dInt
varbaz=Int()
ifbazisInt{print("NiceIntyougotthere")}
Intro to Swift › OpenWest 20160715
Castsvarfoo=1//fooisanInt
varbar=String(foo)//"1"
varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString
varforceBaz=stringishThingas!String
Intro to Swift › OpenWest 20160715
Castsvarfoo=1//fooisanInt
varbar=String(foo)//"1"
varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString
varforceBaz=stringishThingas!String
Intro to Swift › OpenWest 20160715
Castsvarfoo=1//fooisanInt
varbar=String(foo)//"1"
varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString
varforceBaz=stringishThingas!String
Intro to Swift › OpenWest 20160715
Castsvarfoo=1//fooisanInt
varbar=String(foo)//"1"
varmaybeBaz=stringishThingas?String//maybeBazisanoptionallytypedString
varforceBaz=stringishThingas!String
Intro to Swift › OpenWest 20160715
Optional Types//Whenavariablemaynothaveavaluevarbar:Int?
//testifbar!=nil{//hasavalue}
Intro to Swift › OpenWest 20160715
Optional Types//Whenavariablemaynothaveavaluevarbar:Int?
//testifbar!=nil{//hasavalue}
Intro to Swift › OpenWest 20160715
Optional Types//Whenavariablemaynothaveavaluevarbar:Int?
//testifbar!=nil{//hasavalue}
Intro to Swift › OpenWest 20160715
Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}
//unwrappingnil-->runtimeexception!
Intro to Swift › OpenWest 20160715
Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}
//unwrappingnil-->runtimeexception!
Intro to Swift › OpenWest 20160715
Optional Types//unwrapthevaluetouseifbar!=nil{bar!+=2}
//unwrappingnil-->runtimeexception!
Intro to Swift › OpenWest 20160715
if-letvarbar:Int?
ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}
Intro to Swift › OpenWest 20160715
if-letvarbar:Int?
ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}
Intro to Swift › OpenWest 20160715
if-letvarbar:Int?
ifletfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue}else{//barwasnil}
Intro to Swift › OpenWest 20160715
if-varvarbar:Int?
ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}
Intro to Swift › OpenWest 20160715
if-varvarbar:Int?
ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}
Intro to Swift › OpenWest 20160715
if-varvarbar:Int?
ifvarfoo=bar{//barhadavalue&//foonowhasthatunwrappedvalue&//fooismutablefoo++}else{//barwasnil}
Types of Values
Intro to Swift › OpenWest 20160715
Tupleslettuple=("foo",42)
letfirst=tuple.0//"foo"
letlabeledTuple=(one:"foo",two:42)
letsecond=labeledTuple.two//42
Intro to Swift › OpenWest 20160715
Tupleslettuple=("foo",42)
letfirst=tuple.0//"foo"
letlabeledTuple=(one:"foo",two:42)
letsecond=labeledTuple.two//42
Intro to Swift › OpenWest 20160715
Tupleslettuple=("foo",42)
letfirst=tuple.0//"foo"
letlabeledTuple=(one:"foo",two:42)
letsecond=labeledTuple.two//42
Intro to Swift › OpenWest 20160715
Tupleslettuple=("foo",42)
letfirst=tuple.0//"foo"
letlabeledTuple=(one:"foo",two:42)
letsecond=labeledTuple.two//42
Intro to Swift › OpenWest 20160715
Tupleslettuple=("foo",42)
letfirst=tuple.0//"foo"
letlabeledTuple=(one:"foo",two:42)
letsecond=labeledTuple.two//42
Intro to Swift › OpenWest 20160715
Arraysletnums=[1,2,3]
varstrs:[String]
//_can_mix&matchletmixed=[1,"foo"]
//butyouprobablyshouldn't
Intro to Swift › OpenWest 20160715
Arraysletnums=[1,2,3]
varstrs:[String]
//_can_mix&matchletmixed=[1,"foo"]
//butyouprobablyshouldn't
Intro to Swift › OpenWest 20160715
Arraysletnums=[1,2,3]
varstrs:[String]
//_can_mix&matchletmixed=[1,"foo"]
//butyouprobablyshouldn't
Intro to Swift › OpenWest 20160715
Arraysletnums=[1,2,3]
varstrs:[String]
//_can_mix&matchletmixed=[1,"foo"]
//butyouprobablyshouldn't
Intro to Swift › OpenWest 20160715
Arraysletnums=[1,2,3]
varstrs:[String]
//_can_mix&matchletmixed=[1,"foo"]
//butyouprobablyshouldn't
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]
//capitalCityStateshastype//[String:String]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]
//capitalCityStateshastype//[String:String]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":"Utah",]
//capitalCityStateshastype//[String:String]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]
//capitalCityStateshastype//[String:NSObject]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]
//capitalCityStateshastype//[String:NSObject]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]
//capitalCityStateshastype//[String:NSObject]
Intro to Swift › OpenWest 20160715
DictionaryletcapitalCityStates=["Salem":"Oregon","SaltLakeCity":2,]
//capitalCityStateshastype//[String:NSObject]
Intro to Swift › OpenWest 20160715
SetsvarpetSet:Set=["cat","dog","fish","dog"]
petSet.count//returns3
Intro to Swift › OpenWest 20160715
SetsvarpetSet:Set=["cat","dog","fish","dog"]
petSet.count//returns3
Intro to Swift › OpenWest 20160715
SetsvarpetSet:Set=["cat","dog","fish","dog"]
petSet.count//returns3
Intro to Swift › OpenWest 20160715
SetsvarpetSet:Set=["cat","dog","fish","dog"]
petSet.count//returns3
Functions
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(){print("Hello,Salem!")}
//calllike:obExample()
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String){print("Hello,\(who)!")}
//calllikeobExample("OpenWest")
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String){print("Hello,\(who)!")}
//calllikeobExample("OpenWest")
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String){print("Hello,\(who)!")}
//calllikeobExample("OpenWest")
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String)->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("OpenWest")
//greets=="Hello,OpenWest"
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String)->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("OpenWest")
//greets=="Hello,OpenWest"
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"
letgreets=obExample()//"Hello,Salem!"
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"
letgreets=obExample()//"Hello,Salem!"
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"
letgreets=obExample()//"Hello,Salem!"
Intro to Swift › OpenWest 20160715
FunctionsfuncobExample(who:String="Salem")->String{return"Hello,\(who)!"}
//calllikeletgreets=obExample("Oregon")//"Hello,Oregon!"
letgreets=obExample()//"Hello,Salem!"
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
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
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
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
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
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
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
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
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
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!"
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!"
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!"
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!"
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!"
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!"
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!"
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!"
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!"
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!"
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!"
Intro to Swift › OpenWest 20160715
FunctionsfuncvariadiacExample(nums:Int...){//dosomethingwithnums//numsisArray[Int]}
Intro to Swift › OpenWest 20160715
FunctionsfuncvariadiacExample(nums:Int...){//dosomethingwithnums//numsisArray[Int]}
Functions are first-class citizens
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]
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]
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]
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!
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]
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]
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]
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]
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]
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]
OOP is also well supported
Intro to Swift › OpenWest 20160715
ClassesclassDog{
}
Intro to Swift › OpenWest 20160715
PropertiesclassDog{varname:Stringletnoise="WOOF!"}
just variable declarations inside the class
Intro to Swift › OpenWest 20160715
PropertiesclassDog{varname:Stringletnoise="WOOF!"}
Intro to Swift › OpenWest 20160715
PropertiesclassDog{varname:Stringletnoise="WOOF!"}
Class 'Dog' has no initializers
...because you're not allowed to have uninitialized properties.
Intro to Swift › OpenWest 20160715
PropertiesclassDog{varname:String?letnoise="WOOF!"}
you can fix that by making the property an optional type
Intro to Swift › OpenWest 20160715
PropertiesclassDog{varname:String?letnoise="WOOF!"}
you can fix that by making the property an optional type
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
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
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
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
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
Intro to Swift › OpenWest 20160715
MethodsclassDog{varname:Stringletnoise="WOOF"
init(name:String){self.name=name}
funcspeak()->String{returnself.noise}}
Intro to Swift › OpenWest 20160715
MethodsclassDog{varname:Stringletnoise="WOOF"
init(name:String){self.name=name}
funcspeak()->String{returnself.noise}}
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
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
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
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
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
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set(age){//thisishorrible,don'tdothisself.birthYear=currentYear-age}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set(age){//thisishorrible,don'tdothisself.birthYear=currentYear-age}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{get{returncurrentYear-self.birthYear}set{//thisishorrible,don'tdothisself.birthYear=currentYear-newValue}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{willSet{//runsbeforepropertyvaluechanges}didSet{//runsafterpropertyvaluechanges}}}
Intro to Swift › OpenWest 20160715
Computed PropertiesclassDog{varage:Int{willSet{//runsbeforepropertyvaluechanges}didSet{//runsafterpropertyvaluechanges}}}
Inheritance
Intro to Swift › OpenWest 20160715
InheritanceclassAnimal{
}
classDog:Animal{
}
Intro to Swift › OpenWest 20160715
InheritanceclassAnimal{
}
classDog:Animal{
}
Intro to Swift › OpenWest 20160715
InheritanceclassAnimal{letname:String
init(name:name){self.name=name}}
classDog:Animal{init(name:name){super.init(name:name)}}
Intro to Swift › OpenWest 20160715
InheritanceclassAnimal{letname:String
init(name:name){self.name=name}}
classDog:Animal{init(name:name){super.init(name:name)}}
Intro to Swift › OpenWest 20160715
OverridesclassAnimal{funcspeak(){...}}
classDog:Animal{overridefuncspeak(){...}}
Intro to Swift › OpenWest 20160715
OverridesclassAnimal{funcspeak(){...}}
classDog:Animal{overridefuncspeak(){...}}
Structs & Enumerations
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
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
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
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
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}
varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}
varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}
varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs{caseOpenWestcaseYAPC}
varconf=OpenSourceConfs.OpenWest//confistypeOpenSourceConfs
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}
varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs
conf.rawValue//2
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}
varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs
conf.rawValue//2
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}
varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs
conf.rawValue//2
enums provide a constrained set of enumerated values
Intro to Swift › OpenWest 20160715
EnumerationsenumOpenSourceConfs:Int{caseOpenWest=1caseYAPC}
varconf=OpenSourceConfs.YAPC//confistypeOpenSourceConfs
conf.rawValue//2
enums provide a constrained set of enumerated values
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
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
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
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
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
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
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
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
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
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
Intro to Swift › OpenWest 20160715
ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}
Intro to Swift › OpenWest 20160715
ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}
Intro to Swift › OpenWest 20160715
ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}
Intro to Swift › OpenWest 20160715
ProtocolsprotocolTalker{varnoise:String{get}functalk()->String}
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=""}}
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=""}}
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=""}}
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=""}}
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=""}}
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=""}}
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=""}}
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
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
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
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
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
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
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
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
Exceptions & Error Handling
Intro to Swift › OpenWest 20160715
ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}
funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}
Intro to Swift › OpenWest 20160715
ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}
funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}
Intro to Swift › OpenWest 20160715
ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}
funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}
Intro to Swift › OpenWest 20160715
ExceptionsenumTalkErrors:ErrorType{caseTooShortcaseTooLongcaseTooBoring}
funcgiveATalk()throws->String{iftalk=="boring"{throwTalkErrors.TooBoring}return"talk!"}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptionsdo{letthisTalk=trygiveATalk()print(thisTalk)}catchTalkErrors.TooLong{print("shutupalready")}catchlettalkErrorasTalkErrors{print("Talkerror:\(talkError).")}catch{print(error)}
Intro to Swift › OpenWest 20160715
Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()
//thisTalkisaString?
Intro to Swift › OpenWest 20160715
Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()
//thisTalkisaString?
Intro to Swift › OpenWest 20160715
Exceptions//silentlydiscardserrorletthisTalk=try?giveATalk()
//thisTalkisString?
Intro to Swift › OpenWest 20160715
DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}
//othercodehere.}
Intro to Swift › OpenWest 20160715
DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}
//othercodehere.}
Intro to Swift › OpenWest 20160715
DeferfuncneedsMuchSetupAndTearDown()throws{//dothesetuphere,openfiles,etc.defer{//anddothecleanuphere,//rightnexttosetup}
//othercodehere.}
Workspaces
Ill-advisedlive
demotime!
Ill-advisedlive
demotime!
˜
˜ ˜
https://developer.apple.com/swift
This part is a lie.
THANKS!
questions?