SlideShare une entreprise Scribd logo
1  sur  266
Télécharger pour lire hors ligne
A Modest
Introduction
To Swift
The Perl Conference 2017

19 Jun 2017
John SJ Anderson

@genehack
@genehack › Intro to Swift › TPC 2017
Sorry!
@genehack › Intro to Swift › TPC 2017
Hi I'm John
aka @genehack
• VP Tech, Infinity Interactive
• Perl tribe
• Polyglot coder
• Just this guy, you know?
Polyglotism
@genehack › Intro to Swift › TPC 2017
Swift?
@genehack › Intro to Swift › TPC 2017
Introduced in 2014
@genehack › Intro to Swift › TPC 2017
Went Open Source 

at version 2.2
@genehack › Intro to Swift › TPC 2017
Version 3.1 just released

(in March)
@genehack › Intro to Swift › TPC 2017
Version 4 coming

Real Soon Now
@genehack › Intro to Swift › TPC 2017
Originally MacOS only
@genehack › Intro to Swift › TPC 2017
Now on Linux too.
@genehack › Intro to Swift › TPC 2017
Android in the works!
@genehack › Intro to Swift › TPC 2017
Android in the works!DONE!!
@genehack › Intro to Swift › TPC 2017
Windows too?
https://swiftforwindows.codeplex.com/
@genehack › Intro to Swift › TPC 2017
Originally targeted
MacOS, iOS, 

watchOS, and tvOS
@genehack › Intro to Swift › TPC 2017
With expanded platform
support, offers potential
"single-language stack"
advantages a la Node
So what's it like, man?
First, a brief digression…
@genehack › Intro to Swift › TPC 2017
How many MacOS / iOS
users do we have here?
@genehack › Intro to Swift › TPC 2017
How many MacOS / iOS
developers do we have?
@genehack › Intro to Swift › TPC 2017
The dirty little secret of
developing for Apple
@genehack › Intro to Swift › TPC 2017
From In The Beginning
Was The Command Line
by Neal Stephenson
@genehack › Intro to Swift › TPC 2017
Weird Pascal-based
naming and calling
conventions
@genehack › Intro to Swift › TPC 2017
HANDLES?!?
@genehack › Intro to Swift › TPC 2017
ObjectiveC's "syntax"
Swift is the Mac of Apple
programming languages
Swift Syntax
@genehack › Intro to Swift › TPC 2017
Comments
//	this	is	a	comment
@genehack › Intro to Swift › TPC 2017
Comments
/*	this	is	a		
multi-line		
comment	*/
@genehack › Intro to Swift › TPC 2017
Comments
/*	this	is	a		
/*	_nested_		
multi-line	comment,	*/	
which	is	cool!	*/
@genehack › Intro to Swift › TPC 2017
Comments
///	doc	comment
@genehack › Intro to Swift › TPC 2017
Variables
var	foo	=	1	
var	bar:	Int	
var	baz	=	"whee!"
@genehack › Intro to Swift › TPC 2017
Variables
var	foo	=	1	
var	bar:	Int	
var	baz	=	"whee!"
@genehack › Intro to Swift › TPC 2017
Variables
var	foo	=	1	
var	bar:	Int	
var	baz	=	"whee!"
@genehack › Intro to Swift › TPC 2017
Variables
var	foo	=	1	
var	bar:	Int	
var	baz	=	"whee!"
@genehack › Intro to Swift › TPC 2017
Variables
let	bar	=	1	
bar	+=	1		
//	^^	compile	time	error!
@genehack › Intro to Swift › TPC 2017
Variables
let	bar	=	1	
bar	+=	1		
//	^^	compile	time	error!
@genehack › Intro to Swift › TPC 2017
Variables
let	bar		
//	also	a	compile	time	error	
/*		
You	canNOT	have	an	uninitialized	and	
untyped	variable.	You	also	can't	use	
an	uninitialized	variable	_at	all_		
*/
@genehack › Intro to Swift › TPC 2017
Variables
let	bar	
//	also	a	compile	time	error	
/*		
You	canNOT	have	an	uninitialized	and	
untyped	variable.	You	also	can't	use	
an	uninitialized	variable	_at	all_		
*/
How friggin' awesome is that?
@genehack › Intro to Swift › TPC 2017
Operators
@genehack › Intro to Swift › TPC 2017
Flow Control
let	n	=	1	
if	n	>	1	{	
				print("we	got	a	big	N	here")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	n	=	1	
if	n	>	1	{	
				print("we	got	a	big	N	here")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	n	=	1	
if	n	>	1	{	
				print("we	got	a	big	N	here")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	arr	=	[	1,	2,	3,	4]	
var	sum	=	0	
for	elem	in	arr	{	
				sum	+=	elem	
}	
//	sum	now	is	10
@genehack › Intro to Swift › TPC 2017
Flow Control
let	arr	=	[	1,	2,	3,	4]	
var	sum	=	0	
for	elem	in	arr	{	
				sum	+=	elem	
}	
//	sum	now	is	10
@genehack › Intro to Swift › TPC 2017
Flow Control
let	arr	=	[	1,	2,	3,	4]	
var	sum	=	0	
for	elem	in	arr	{	
				sum	+=	elem	
}	
//	sum	now	is	10
@genehack › Intro to Swift › TPC 2017
Flow Control
let	arr	=	[	1,	2,	3,	4]	
var	sum	=	0	
for	elem	in	arr	{	
				sum	+=	elem	
}	
//	sum	now	is	10
@genehack › Intro to Swift › TPC 2017
Flow Control
for	index	in	1	...	10	{	
				#	do	something	10	times	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
for	index	in	1	...	10	{	
				#	do	something	10	times	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
for	index	in	1	..<	10	{	
				#	do	something	9	times	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
for	index	in	1	..<	10	{	
				#	do	something	9	times	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countDown	=	5	
while	countDown	>	0	{	
				countDown--	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countDown	=	5	
while	countDown	>	0	{	
				countDown--	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countDown	=	5	
while	countDown	>	0	{	
				countDown	-=	1	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countDown	=	5	
while	countDown	>	0	{	
				countDown	-=	1	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countUp	=	0	
repeat	{	
				countUp++	
}	while	countUp	<	5
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countUp	=	0	
repeat	{	
				countUp++	
}	while	countUp	<	5
@genehack › Intro to Swift › TPC 2017
Flow Control
var	countUp	=	0	
repeat	{	
				countUp	+=	1	
}	while	countUp	<	5
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	2	
switch	sample	{	
case	0:		
				print("Is	0")	
case	2:		
				print("Is	2")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	0	or	2,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	2	
switch	sample	{	
case	0:		
				print("Is	0")	
case	2:		
				print("Is	2")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	0	or	2,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	2	
switch	sample	{	
case	0:		
				print("Is	0")	
case	2:		
				print("Is	2")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	0	or	2,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	2	
switch	sample	{	
case	0:		
				print("Is	0")	
case	2:		
				print("Is	2")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	0	or	2,	is	it.")	
}
No fallthru by default!
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	2	
switch	sample	{	
case	0:		
				print("Is	0")	
case	2:		
				print("Is	2")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	0	or	2,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	"foo"	
switch	sample	{	
case	"foo":		
				print("Is	foo")	
case	"bar":		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	foo	or	bar,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	"foo"	
switch	sample	{	
case	"foo":		
				print("Is	foo")	
case	"bar":		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("Not	foo	or	bar,	is	it.")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	2):		
				print("Is	foo,	2")	
case	("bar",	_):		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("	¯_(ツ)_/¯	")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	2):		
				print("Is	foo,	2")	
case	("bar",	_):		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("	¯_(ツ)_/¯	")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	2):		
				print("Is	foo,	2")	
case	("bar",	_):		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("	¯_(ツ)_/¯	")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	2):		
				print("Is	foo,	2")	
case	("bar",	_):		
				print("Is	bar")	
default:	//	mandatory	when	cases	not	exclusive	
				print("	¯_(ツ)_/¯	")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	3):		
				print("Is	foo,	3")	
case	(let	one,	let	two):		
				print("Is	(one)	and	(two)")	
}
@genehack › Intro to Swift › TPC 2017
Flow Control
let	sample	=	("foo",	2)	
switch	sample	{	
case	("foo",	3):		
				print("Is	foo,	3")	
case	(let	one,	let	two):		
				print("Is	(one)	and	(two)")	
}
@genehack › Intro to Swift › TPC 2017
Strings
var	myString	=	"this	is	a	string"	
if	myString.isEmpty	{		
				//	do	something	
}	
myString	+=	"and	this	is	a	longer	string"
@genehack › Intro to Swift › TPC 2017
Strings
var	myString	=	"this	is	a	string"	
if	myString.isEmpty	{		
				//	do	something	
}	
myString	+=	"and	this	is	a	longer	string"
@genehack › Intro to Swift › TPC 2017
Strings
var	myString	=	"this	is	a	string"	
if	myString.isEmpty	{		
				//	do	something	
}	
myString	+=	"and	this	is	a	longer	string"
Swift is very strongly typed.
@genehack › Intro to Swift › TPC 2017
Typing
var	foo	=	1		//	foo	is	an	Int	
var	bar:	Int	//	bar	is	an	uninit'd	Int		
var	baz	=	Int()	
if	baz	is	Int	{		
				print("Nice	Int	you	got	there")	
}
@genehack › Intro to Swift › TPC 2017
Typing
var	foo	=	1		//	foo	is	an	Int	
var	bar:	Int	//	bar	is	an	uninit'd	Int		
var	baz	=	Int()	
if	baz	is	Int	{		
				print("Nice	Int	you	got	there")	
}
@genehack › Intro to Swift › TPC 2017
Typing
var	foo	=	1		//	foo	is	an	Int	
var	bar:	Int	//	bar	is	an	uninit'd	Int		
var	baz	=	Int()	
if	baz	is	Int	{		
				print("Nice	Int	you	got	there")	
}
@genehack › Intro to Swift › TPC 2017
Typing
var	foo	=	1		//	foo	is	an	Int	
var	bar:	Int	//	bar	is	an	uninit'd	Int		
var	baz	=	Int()	
if	baz	is	Int	{		
				print("Nice	Int	you	got	there")	
}
@genehack › Intro to Swift › TPC 2017
Typing
var	foo	=	1		//	foo	is	an	Int	
var	bar:	Int	//	bar	is	an	uninit'd	Int		
var	baz	=	Int()	
if	baz	is	Int	{		
				print("Nice	Int	you	got	there")	
}
@genehack › Intro to Swift › TPC 2017
Casts
var	foo	=	1	//	foo	is	an	Int	
var	bar	=	String(foo)	//	"1"	
var	maybeBaz	=	stringishThing	as?	String	
//	maybeBaz	is	an	optionally	typed	String	
var	forceBaz	=	stringishThing	as!	String
@genehack › Intro to Swift › TPC 2017
Casts
var	foo	=	1	//	foo	is	an	Int	
var	bar	=	String(foo)	//	"1"	
var	maybeBaz	=	stringishThing	as?	String	
//	maybeBaz	is	an	optionally	typed	String	
var	forceBaz	=	stringishThing	as!	String
@genehack › Intro to Swift › TPC 2017
Casts
var	foo	=	1	//	foo	is	an	Int	
var	bar	=	String(foo)	//	"1"	
var	maybeBaz	=	stringishThing	as?	String	
//	maybeBaz	is	an	optionally	typed	String	
var	forceBaz	=	stringishThing	as!	String
@genehack › Intro to Swift › TPC 2017
Casts
var	foo	=	1	//	foo	is	an	Int	
var	bar	=	String(foo)	//	"1"	
var	maybeBaz	=	stringishThing	as?	String	
//	maybeBaz	is	an	optionally	typed	String	
var	forceBaz	=	stringishThing	as!	String
@genehack › Intro to Swift › TPC 2017
Optional Types
//	When	a	variable	may	not	have	a	value	
var	bar:	Int?	
//	test	
if	bar	!=	nil	{		
				//	has	a	value	
}
@genehack › Intro to Swift › TPC 2017
Optional Types
//	When	a	variable	may	not	have	a	value	
var	bar:	Int?	
//	test	
if	bar	!=	nil	{		
				//	has	a	value	
}
@genehack › Intro to Swift › TPC 2017
Optional Types
//	When	a	variable	may	not	have	a	value	
var	bar:	Int?	
//	test	
if	bar	!=	nil	{		
				//	has	a	value	
}
@genehack › Intro to Swift › TPC 2017
Optional Types
//	unwrap	the	value	to	use	
if	bar	!=	nil	{		
		bar!	+=	2	
}	
//	unwrapping	nil	-->	runtime	exception!
@genehack › Intro to Swift › TPC 2017
Optional Types
//	unwrap	the	value	to	use	
if	bar	!=	nil	{		
		bar!	+=	2	
}	
//	unwrapping	nil	-->	runtime	exception!
@genehack › Intro to Swift › TPC 2017
Optional Types
//	unwrap	the	value	to	use	
if	bar	!=	nil	{		
		bar!	+=	2	
}	
//	unwrapping	nil	-->	runtime	exception!
@genehack › Intro to Swift › TPC 2017
if-let
var	bar:	Int?	
if	let	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	
}	
else	{		
				//	bar	was	nil	
}
@genehack › Intro to Swift › TPC 2017
if-let
var	bar:	Int?	
if	let	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	
}	
else	{		
				//	bar	was	nil	
}
@genehack › Intro to Swift › TPC 2017
if-let
var	bar:	Int?	
if	let	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	
}	
else	{		
				//	bar	was	nil	
}
@genehack › Intro to Swift › TPC 2017
if-var
var	bar:	Int?	
if	var	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	&	
				//	foo	is	mutable	
				foo	+=	1	
}	
else	{		
				//	bar	was	nil	
}
@genehack › Intro to Swift › TPC 2017
if-var
var	bar:	Int?	
if	var	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	&	
				//	foo	is	mutable	
				foo	+=	1	
}	
else	{		
				//	bar	was	nil	
}
@genehack › Intro to Swift › TPC 2017
if-var
var	bar:	Int?	
if	var	foo	=	bar	{	
				//	bar	had	a	value	&		
				//	foo	now	has	that	unwrapped	value	&	
				//	foo	is	mutable	
				foo	+=	1	
}	
else	{		
				//	bar	was	nil	
}
Types of Values
@genehack › Intro to Swift › TPC 2017
Tuples
let	tuple	=	("foo",	42)	
let	first	=	tuple.0	//	"foo"	
let	labeledTuple	=	(one:	"foo",	two:	42)	
let	second	=	labeledTuple.two	//	42
@genehack › Intro to Swift › TPC 2017
Tuples
let	tuple	=	("foo",	42)	
let	first	=	tuple.0	//	"foo"	
let	labeledTuple	=	(one:	"foo",	two:	42)	
let	second	=	labeledTuple.two	//	42
@genehack › Intro to Swift › TPC 2017
Tuples
let	tuple	=	("foo",	42)	
let	first	=	tuple.0	//	"foo"	
let	labeledTuple	=	(one:	"foo",	two:	42)	
let	second	=	labeledTuple.two	//	42
@genehack › Intro to Swift › TPC 2017
Tuples
let	tuple	=	("foo",	42)	
let	first	=	tuple.0	//	"foo"	
let	labeledTuple	=	(one:	"foo",	two:	42)	
let	second	=	labeledTuple.two	//	42
@genehack › Intro to Swift › TPC 2017
Tuples
let	tuple	=	("foo",	42)	
let	first	=	tuple.0	//	"foo"	
let	labeledTuple	=	(one:	"foo",	two:	42)	
let	second	=	labeledTuple.two	//	42
@genehack › Intro to Swift › TPC 2017
Arrays
let	nums	=	[1,	2,	3]	
var	strs	:	[String]	
//	_can_	mix	&	match	
let	mixed	=	[1,	"foo"]	
//	but	you	probably	shouldn't	
//	in	Swift	3+	this	is	a	compile		
//	error	unless	specifically		
//	type-annotated	
let	mixed:	[Any]	=	[1,	"foo"]
@genehack › Intro to Swift › TPC 2017
Arrays
let	nums	=	[1,	2,	3]	
var	strs	:	[String]	
//	_can_	mix	&	match	
let	mixed	=	[1,	"foo"]	
//	but	you	probably	shouldn't	
//	in	Swift	3+	this	is	a	compile		
//	error	unless	specifically		
//	type-annotated	
let	mixed:	[Any]	=	[1,	"foo"]
@genehack › Intro to Swift › TPC 2017
Arrays
let	nums	=	[1,	2,	3]	
var	strs	:	[String]	
//	_can_	mix	&	match	
let	mixed	=	[1,	"foo"]	
//	but	you	probably	shouldn't	
//	in	Swift	3+	this	is	a	compile		
//	error	unless	specifically		
//	type-annotated	
let	mixed:	[Any]	=	[1,	"foo"]
@genehack › Intro to Swift › TPC 2017
Arrays
let	nums	=	[1,	2,	3]	
var	strs	:	[String]	
//	_can_	mix	&	match	
let	mixed	=	[1,	"foo"]	
//	but	you	probably	shouldn't	
//	in	Swift	3+	this	is	a	compile		
//	error	unless	specifically		
//	type-annotated	
let	mixed:	[Any]	=	[1,	"foo"]
@genehack › Intro to Swift › TPC 2017
Arrays
let	nums	=	[1,	2,	3]	
var	strs	:	[String]	
//	_can_	mix	&	match	
let	mixed	=	[1,	"foo"]	
//	but	you	probably	shouldn't	
//	in	Swift	3+	this	is	a	compile		
//	error	unless	specifically		
//	type-annotated	
let	mixed:	[Any]	=	[1,	"foo"]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	"Virginia",	
]	
//	capitalCityStates	has	type		
//	[String:String]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	"Virginia",	
]	
//	capitalCityStates	has	type		
//	[String:String]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	"Virginia",	
]	
//	capitalCityStates	has	type		
//	[String:String]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	2,	
]	
//	capitalCityStates	must	be		
//	annotated	with	type		
//	[String:Any]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	2,	
]	
//	capitalCityStates	must	be		
//	annotated	with	type		
//	[String:Any]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates	=	[	
		"Salem":	"Oregon",	
		"Richmond":	2,	
]	
//	capitalCityStates	must	be		
//	annotated	with	type		
//	[String:Any]
@genehack › Intro to Swift › TPC 2017
Dictionary
let	capitalCityStates:	[String:Any]	=	[	
		"Salem":	"Oregon",	
		"Richmond":	2,	
]	
//	capitalCityStates	must	be		
//	annotated	with	type	[String:Any]
@genehack › Intro to Swift › TPC 2017
Sets
var	petSet	:Set	=	[	"cat",	"dog",		
																				"fish",	"dog"]	
petSet.count	//	returns	3
@genehack › Intro to Swift › TPC 2017
Sets
var	petSet	:Set	=	[	"cat",	"dog",		
																				"fish",	"dog"]	
petSet.count	//	returns	3
@genehack › Intro to Swift › TPC 2017
Sets
var	petSet	:Set	=	[	"cat",	"dog",		
																				"fish",	"dog"]	
petSet.count	//	returns	3
@genehack › Intro to Swift › TPC 2017
Sets
var	petSet	:Set	=	[	"cat",	"dog",		
																				"fish",	"dog"]	
petSet.count	//	returns	3
Functions
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	()	{	
		print("Hello,	Perl!")	
}	
//	call	like:	
obExample()
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String)	{	
		print("Hello,	(who)!")	
}	
//	call	like	
obExample(who:	"The	Perl	Conference")
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String)	{	
		print("Hello,	(who)!")	
}	
//	call	like	
obExample(who:	"The	Perl	Conference")
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String)	{	
		print("Hello,	(who)!")	
}	
//	call	like	
obExample(who:	"The	Perl	Conference")
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String)	->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample("TPC")	
//	greets	==	"Hello,	TPC"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String)	->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample("TPC")	
//	greets	==	"Hello,	TPC"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String	=	"Swift")		
				->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample(who:"Perl")		
//	"Hello,	Perl!"	
let	defGreets	=	obExample()	
//	"Hello,	Swift!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String	=	"Swift")		
				->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample(who:"Perl")		
//	"Hello,	Perl!"	
let	defGreets	=	obExample()	
//	"Hello,	Swift!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String	=	"Swift")		
				->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample(who:"Perl")		
//	"Hello,	Perl!"	
let	defGreets	=	obExample()	
//	"Hello,	Swift!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(who	:String	=	"Swift")		
				->	String	{	
		return	"Hello,	(who)!"	
}	
//	call	like	
let	greets	=	obExample(who:"Perl")		
//	"Hello,	Perl!"	
let	defGreets	=	obExample()	
//	"Hello,	Swift!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																who	them	:String	=	"Perl")	{	
		print("(what),	(them)!")	
}	
//	call	like		
obExample(what:	"bye")																	//	"bye,	Perl!"	
obExample(what:	"bye",	who:	"Felicia")	//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																who	them	:String	=	"Perl")	{	
		print("(what),	(them)!")	
}	
//	call	like		
obExample(what:	"bye")																	//	"bye,	Perl!"	
obExample(what:	"bye",	who:	"Felicia")	//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																who	them	:String	=	"Perl")	{	
		print("(what),	(them)!")	
}	
//	call	like		
obExample(what:	"bye")																	//	"bye,	Perl!"	
obExample(what:	"bye",	who:	"Felicia")	//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																who	them	:String	=	"Perl")	{	
		print("(what),	(them)!")	
}	
//	call	like		
obExample(what:	"bye")																	//	"bye,	Perl!"	
obExample(what:	"bye",	who:	"Felicia")	//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																who	them	:String	=	"Perl")	{	
		print("(what),	(them)!")	
}	
//	call	like		
obExample(what:	"bye")																	//	"bye,	Perl!"	
obExample(what:	"bye",	who:	"Felicia")	//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	obExample	(what	:String	=	"Hello",		
																_	who	:String	=	"Perl")	{	
		print	"(what),	(who)!"	
}	
//	call	like	
obExample(what:	"bye")																//	"bye,	Salem!"	
obExample(what:	"bye",	who:"Felicia")	//	COMPILE	ERROR	
obExample(what:	"bye",	"Felicia")					//	"bye,	Felicia!"
@genehack › Intro to Swift › TPC 2017
Functions
func	variadiacExample	(nums:	Int...)		{	
				//	do	something	with	nums	
				//	nums	is	Array[Int]	
}
@genehack › Intro to Swift › TPC 2017
Functions
func	variadiacExample	(nums:	Int...)		{	
				//	do	something	with	nums	
				//	nums	is	Array[Int]	
}
Functions are first-class citizens
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
var	sorted	=	numbers.sorted(by:{		
		(n1:	Int,	n2:	Int)	->	Bool	in	return	n2	>	n1		
})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
var	sorted	=	numbers.sorted(by:{		
		(n1:	Int,	n2:	Int)	->	Bool	in	return	n2	>	n1		
})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
var	sorted	=	numbers.sorted(by:{		
		(n1:	Int,	n2:	Int)	->	Bool	in	return	n2	>	n1		
})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
var	sorted	=	numbers.sorted(by:{		
		(n1:	Int,	n2:	Int)	->	Bool	in	return	n2	>	n1		
})		
//	sorted	=	[1,	2,	13,	32,	56,	120]	
This is already the func sig for sort!
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	inferred	param	&	return	types	
var	sorted	=	numbers.sorted(by:	{n1,	n2	in	return	n2	>	n1})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	inferred	param	&	return	types	
var	sorted	=	numbers.sorted(by:	{n1,	n2	in	return	n2	>	n1})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	positionally	named	parameters	
var	sorted	=	numbers.sorted(by:	{return	$0	>	$1})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	positionally	named	parameters	
var	sorted	=	numbers.sorted(by:	{return	$0	>	$1})		
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	when	closure	is	last	param,		
//	param	name	&	parens	optional	
var	sorted	=	numbers.sorted	{	$0	>	$1	}	
//	sorted	=	[1,	2,	13,	32,	56,	120]
@genehack › Intro to Swift › TPC 2017
Closures
let	numbers	=	[2,1,56,32,120,13]		
//	when	closure	is	last	param,		
//	param	name	&	parens	optional	
var	sorted	=	numbers.sorted	{	$0	>	$1	}	
//	sorted	=	[1,	2,	13,	32,	56,	120]
OOP is also
well supported
@genehack › Intro to Swift › TPC 2017
Classes
class	Dog	{	
}
@genehack › Intro to Swift › TPC 2017
Properties
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF!"	
}
@genehack › Intro to Swift › TPC 2017
Properties
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF!"	
}
@genehack › Intro to Swift › TPC 2017
Properties
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF!"	
}
Class 'Dog' has no initializers
@genehack › Intro to Swift › TPC 2017
Properties
class	Dog	{	
				var	name:	String?	
				let	noise	=	"WOOF!"	
}
@genehack › Intro to Swift › TPC 2017
Properties
class	Dog	{	
				var	name:	String?	
				let	noise	=	"WOOF!"	
}
@genehack › Intro to Swift › TPC 2017
Initializers
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
}
@genehack › Intro to Swift › TPC 2017
Initializers
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
}
@genehack › Intro to Swift › TPC 2017
Initializers
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
}
@genehack › Intro to Swift › TPC 2017
Deinitializers
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
				deinit	()	{	
								//	do	any	cleanup	here	
				}	
}
@genehack › Intro to Swift › TPC 2017
Deinitializers
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
				deinit	()	{	
								//	do	any	cleanup	here	
				}	
}
@genehack › Intro to Swift › TPC 2017
Methods
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
				func	speak	()	->	String	{	
								return	self.noise	
				}	
}
@genehack › Intro to Swift › TPC 2017
Methods
class	Dog	{	
				var	name:	String	
				let	noise	=	"WOOF"	
				init	(name:	String)	{	
								self.name	=	name	
				}	
				func	speak	()	->	String	{	
								return	self.noise	
				}	
}
@genehack › Intro to Swift › TPC 2017
Using Objects
let	sammy	=	Dog(name:	"Sammy");		
//	sammy	is	Dog	
print(sammy.name)					//	prints	"Sammyn"	
print(sammy.speak())		//	prints	"WOOF!n"	
sammy.name	=	"Buster"	//	works	b/c	prop	is	var
@genehack › Intro to Swift › TPC 2017
Using Objects
let	sammy	=	Dog(name:	"Sammy");		
//	sammy	is	Dog	
print(sammy.name)					//	prints	"Sammyn"	
print(sammy.speak())		//	prints	"WOOF!n"	
sammy.name	=	"Buster"	//	works	b/c	prop	is	var
@genehack › Intro to Swift › TPC 2017
Using Objects
let	sammy	=	Dog(name:	"Sammy");		
//	sammy	is	Dog	
print(sammy.name)					//	prints	"Sammyn"	
print(sammy.speak())		//	prints	"WOOF!n"	
sammy.name	=	"Buster"	//	works	b/c	prop	is	var
@genehack › Intro to Swift › TPC 2017
Using Objects
let	sammy	=	Dog(name:	"Sammy");		
//	sammy	is	Dog	
print(sammy.name)					//	prints	"Sammyn"	
print(sammy.speak())		//	prints	"WOOF!n"	
sammy.name	=	"Buster"	//	works	b/c	prop	is	var
@genehack › Intro to Swift › TPC 2017
Using Objects
let	sammy	=	Dog(name:	"Sammy");		
//	sammy	is	Dog	
print(sammy.name)					//	prints	"Sammyn"	
print(sammy.speak())		//	prints	"WOOF!n"	
sammy.name	=	"Buster"	//	works	b/c	prop	is	var
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	newValue	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	newValue	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	newValue	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	newValue	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	(age)	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	age	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	(age)	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	age	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				get	{	
						return	currentYear	-	self.birthYear	
				}	
				set	{	
						//	this	is	horrible,	don't	do	this	
						self.birthYear	=	currentYear	-	age	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				willSet	{	
						//	runs	before	property	value	changes	
				}	
				didSet	{	
						//	runs	after	property	value	changes	
				}	
		}	
}
@genehack › Intro to Swift › TPC 2017
Computed Properties
class	Dog	{	
		var	age	:Int	{	
				willSet	{	
						//	runs	before	property	value	changes	
				}	
				didSet	{	
						//	runs	after	property	value	changes	
				}	
		}	
}
Inheritance
@genehack › Intro to Swift › TPC 2017
Inheritance
class	Animal	{	
}	
class	Dog	:	Animal	{	
}
@genehack › Intro to Swift › TPC 2017
Inheritance
class	Animal	{	
}	
class	Dog	:	Animal	{	
}
@genehack › Intro to Swift › TPC 2017
Inheritance
class	Animal	{	
		let	name:	String	
		init	(name:	name)	{		
				self.name	=	name	
		}	
}	
class	Dog	:	Animal	{	
		override	init	(name:	name)	{	
				super.init(name:	name)	
		}	
}
@genehack › Intro to Swift › TPC 2017
Inheritance
class	Animal	{	
		let	name:	String	
		init	(name:	name)	{		
				self.name	=	name	
		}	
}	
class	Dog	:	Animal	{	
		override	init	(name:	name)	{	
				super.init(name:	name)	
		}	
}
@genehack › Intro to Swift › TPC 2017
Inheritance
class	Animal	{	
		let	name:	String	
		init	(name:	name)	{		
				self.name	=	name	
		}	
}	
class	Dog	:	Animal	{	
		override	init	(name:	name)	{	
				super.init(name:	name)	
		}	
}
@genehack › Intro to Swift › TPC 2017
Overrides
class	Animal	{	
		func	speak()	{	...	}	
}	
class	Dog	:	Animal	{	
		override	func	speak	()	{	...	}	
}
@genehack › Intro to Swift › TPC 2017
Overrides
class	Animal	{	
		func	speak()	{	...	}	
}	
class	Dog	:	Animal	{	
		override	func	speak	()	{	...	}	
}
Structs & Enumerations
@genehack › Intro to Swift › TPC 2017
Structs
struct	Animal	{	
		var	name:	String	
		var	noise:	String	
		init	(name:	String,	makes:	String)	{	
				self.name	=	name	
				noise	=	makes	
		}	
		func	speak	()	->	String	{	
				return	noise	
		}	
}
@genehack › Intro to Swift › TPC 2017
Structs
struct	Animal	{	
		var	name:	String	
		var	noise:	String	
		init	(name:	String,	makes:	String)	{	
				self.name	=	name	
				noise	=	makes	
		}	
		func	speak	()	->	String	{	
				return	noise	
		}	
}
@genehack › Intro to Swift › TPC 2017
Structs
struct	Animal	{	
		var	name:	String	
		var	noise:	String	
		init	(name:	String,	makes:	String)	{	
				self.name	=	name	
				noise	=	makes	
		}	
		func	speak	()	->	String	{	
				return	noise	
		}	
}
@genehack › Intro to Swift › TPC 2017
Structs
struct	Animal	{	
		var	name:	String	
		var	noise:	String	
		init	(name:	String,	makes:	String)	{	
				self.name	=	name	
				noise	=	makes	
		}	
		func	speak	()	->	String	{	
				return	noise	
		}	
}
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.TPC		
//	conf	is	type	OpenSourceConfs
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.TPC	
//	conf	is	type	OpenSourceConfs
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.TPC	
//	conf	is	type	OpenSourceConfs
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.TPC		
//	conf	is	type	OpenSourceConfs
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	:Int	{	
		case	TPC	=	1	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.YAPC		
//	conf	is	type	OpenSourceConfs	
conf.rawValue	//	2
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	:Int	{	
		case	TPC	=	1	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.YAPC		
//	conf	is	type	OpenSourceConfs	
conf.rawValue	//	2
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	:Int	{	
		case	TPC	=	1	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.YAPC		
//	conf	is	type	OpenSourceConfs	
conf.rawValue	//	2
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	:Int	{	
		case	TPC	=	1	
		case	YAPC	
}	
var	conf	=	OpenSourceConfs.YAPC		
//	conf	is	type	OpenSourceConfs	
conf.rawValue	//	2
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC:		
								return	"hello	virginia!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC:		
								return	"hello	virginia!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC:		
								return	"hello	virginia!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC:		
								return	"hello	virginia!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC(let	location):		
								return	"hello	(location)!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC("DC")	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC(let	location):		
								return	"hello	(location)!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC("DC")	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC(let	location):		
								return	"hello	(location)!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC("DC")	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC(let	location):		
								return	"hello	(location)!"	
						case	.YAPC:	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	conf	=	OpenSourceConfs.TPC("DC")	
conf.describe()
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC	(let	location):		
								return	"hello	(location)!"	
						case	.YAPC(location)	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	tpc2017	=	OpenSourceConfs.TPC("DC")		
var	tpc2018	=	OpenSourceConfs.TPC("Pittsurgh?")
@genehack › Intro to Swift › TPC 2017
Enumerations
enum	OpenSourceConfs	{	
		case	TPC(String)	
		case	YAPC	
		func	describe()	->	String	{	
				switch	self	{	
						case	.TPC	(let	location):		
								return	"hello	(location)!"	
						case	.YAPC(location)	
								return	"I	thought	YAPC	was	dead"	
				}	
		}	
}	
var	tpc2017	=	OpenSourceConfs.TPC("DC")		
var	tpc2018	=	OpenSourceConfs.TPC("Pittsurgh?")
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	
		var	noise:	String	{	get	}	
		func	talk	()	->	String	
		mutating	func	mute()	
}	
class	Dog:	Talker	{	
		var	noise:	String	
		init	(noise:	String)	{		
				self.noise	=	noise	
		}	
		func	talk	()	->	String	{		
				return	noise	
		}	
		func	mute	()	{		
				noise	=	""	
		}	
}
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	...	}	
class	Dog:	Talker	{	...	}	
class	Fish	{	...	}		
var	sammy:	Talker	
sammy	=	Dog(noise:	"WOOF!")	
sammy	=	Fish()		//	compile	error
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	...	}	
class	Dog:	Talker	{	...	}	
class	Fish	{	...	}		
var	sammy:	Talker	
sammy	=	Dog(noise:	"WOOF!")	
sammy	=	Fish()		//	compile	error
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	...	}	
class	Dog:	Talker	{	...	}	
class	Fish	{	...	}		
var	sammy:	Talker	
sammy	=	Dog(noise:	"WOOF!")	
sammy	=	Fish()		//	compile	error
@genehack › Intro to Swift › TPC 2017
Protocols
protocol	Talker	{	...	}	
class	Dog:	Talker	{	...	}	
class	Fish	{	...	}		
var	sammy:	Talker	
sammy	=	Dog(noise:	"WOOF!")	
sammy	=	Fish()		//	compile	error
@genehack › Intro to Swift › TPC 2017
Extensions
extension	Int	{	
		func	squared	()	->	Int	{	
				return	self	*	self	
		}	
}	
let	foo	=	2		
print(foo.squared())											//		4		
print(foo.squared().squared())	//	16
@genehack › Intro to Swift › TPC 2017
Extensions
extension	Int	{	
		func	squared	()	->	Int	{	
				return	self	*	self	
		}	
}	
let	foo	=	2		
print(foo.squared())											//		4		
print(foo.squared().squared())	//	16
@genehack › Intro to Swift › TPC 2017
Extensions
extension	Int	{	
		func	squared	()	->	Int	{	
				return	self	*	self	
		}	
}	
let	foo	=	2		
print(foo.squared())											//		4		
print(foo.squared().squared())	//	16
@genehack › Intro to Swift › TPC 2017
Extensions
extension	Int	{	
		func	squared	()	->	Int	{	
				return	self	*	self	
		}	
}	
let	foo	=	2		
print(foo.squared())											//		4		
print(foo.squared().squared())	//	16
Exceptions & Error Handling
@genehack › Intro to Swift › TPC 2017
Exceptions
enum	TalkErrors:	Error	{	
		case	TooShort	
		case	TooLong	
		case	TooBoring	
}	
func	giveATalk	(talk:	String)	throws	->	String	{	
		if	talk	==	"boring"	{	
				throw	TalkErrors.TooBoring	
		}	
		return	"talk!"	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
enum	TalkErrors:	Error	{	
		case	TooShort	
		case	TooLong	
		case	TooBoring	
}	
func	giveATalk	(talk:	String)	throws	->	String	{	
		if	talk	==	"boring"	{	
				throw	TalkErrors.TooBoring	
		}	
		return	"talk!"	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
enum	TalkErrors:	Error	{	
		case	TooShort	
		case	TooLong	
		case	TooBoring	
}	
func	giveATalk	(talk:	String)	throws	->	String	{	
		if	talk	==	"boring"	{	
				throw	TalkErrors.TooBoring	
		}	
		return	"talk!"	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
enum	TalkErrors:	Error	{	
		case	TooShort	
		case	TooLong	
		case	TooBoring	
}	
func	giveATalk	(talk:	String)	throws	->	String	{	
		if	talk	==	"boring"	{	
				throw	TalkErrors.TooBoring	
		}	
		return	"talk!"	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"boring")	
		print(thisTalk)	
}	
catch	{	
		print(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"boring")	
		print(thisTalk)	
}	
catch	{	
		print(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"boring")	
		print(thisTalk)	
}	
catch	{	
		print(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"boring")	
		print(thisTalk)	
}	
catch	{	
		print(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"fine")	
		print(thisTalk)	
}	
catch	TalkErrors.TooLong	{	
		print("shut	up	already")	
}	
catch	let	talkError	as	TalkErrors	{	
		print("Talk	error:	(talkError).")	
}	
catch	{	
		print	(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"fine")	
		print(thisTalk)	
}	
catch	TalkErrors.TooLong	{	
		print("shut	up	already")	
}	
catch	let	talkError	as	TalkErrors	{	
		print("Talk	error:	(talkError).")	
}	
catch	{	
		print	(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"fine")	
		print(thisTalk)	
}	
catch	TalkErrors.TooLong	{	
		print("shut	up	already")	
}	
catch	let	talkError	as	TalkErrors	{	
		print("Talk	error:	(talkError).")	
}	
catch	{	
		print	(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"fine")	
		print(thisTalk)	
}	
catch	TalkErrors.TooLong	{	
		print("shut	up	already")	
}	
catch	let	talkError	as	TalkErrors	{	
		print("Talk	error:	(talkError).")	
}	
catch	{	
		print	(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
do	{	
		let	thisTalk	=	try	giveATalk(talk:	"fine")	
		print(thisTalk)	
}	
catch	TalkErrors.TooLong	{	
		print("shut	up	already")	
}	
catch	let	talkError	as	TalkErrors	{	
		print("Talk	error:	(talkError).")	
}	
catch	{	
		print	(error)	
}
@genehack › Intro to Swift › TPC 2017
Exceptions
//	silently	discards	error	
let	thisTalk	=	try?	giveATalk(talk:"fine")	
//	thisTalk	isa	String?
@genehack › Intro to Swift › TPC 2017
Exceptions
//	silently	discards	error	
let	thisTalk	=	try?	giveATalk(talk:"fine")	
//	thisTalk	isa	String?
@genehack › Intro to Swift › TPC 2017
Exceptions
//	silently	discards	error	
let	thisTalk	=	try?	giveATalk(talk:"fine")	
//	thisTalk	isa	String?
@genehack › Intro to Swift › TPC 2017
Defer
func	needsMuchSetupAndTearDown	()	{	
		//	do	the	setup	here,	open	files,	etc.	
		defer	{	
				//	and	do	the	cleanup	here,		
				//	right	next	to	set	up		
		}	
		//	other	code	here.		
}
@genehack › Intro to Swift › TPC 2017
Defer
func	needsMuchSetupAndTearDown	()	{	
		//	do	the	setup	here,	open	files,	etc.	
		defer	{	
				//	and	do	the	cleanup	here,		
				//	right	next	to	set	up		
		}	
		//	other	code	here.		
}
Workspaces
Ill-advised

live

demo

time!
Ill-advised

live

demo

time!
https://developer.apple.com/swift
THANKS!
questions?

Contenu connexe

Tendances

Project Priority Status Report
Project Priority Status ReportProject Priority Status Report
Project Priority Status ReportDemand Metric
 
QCon London 2019 - LinuxKit
QCon London 2019 - LinuxKitQCon London 2019 - LinuxKit
QCon London 2019 - LinuxKitAvi Deitcher
 
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code ReviewJenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code ReviewLuca Milanesio
 
How to keep Jenkins logs forever without performance issues
How to keep Jenkins logs forever without performance issuesHow to keep Jenkins logs forever without performance issues
How to keep Jenkins logs forever without performance issuesLuca Milanesio
 

Tendances (6)

True Git
True Git True Git
True Git
 
Project Priority Status Report
Project Priority Status ReportProject Priority Status Report
Project Priority Status Report
 
QCon London 2019 - LinuxKit
QCon London 2019 - LinuxKitQCon London 2019 - LinuxKit
QCon London 2019 - LinuxKit
 
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code ReviewJenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
Jenkins world 2017 - Data-Driven CI Pipeline with Gerrit Code Review
 
How to keep Jenkins logs forever without performance issues
How to keep Jenkins logs forever without performance issuesHow to keep Jenkins logs forever without performance issues
How to keep Jenkins logs forever without performance issues
 
Pluginize ALL the things
Pluginize ALL the thingsPluginize ALL the things
Pluginize ALL the things
 

Similaire à A Modest Introduction To Swift

A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to SwiftJohn Anderson
 
A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to SwiftAll Things Open
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To SwiftJohn Anderson
 
A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to SwiftJohn Anderson
 
Logs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youLogs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youJohn Anderson
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side SwiftJens Ravens
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side SwiftChad Moone
 
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...Etiene Dalcol
 
Perl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Lintermoznion
 
Free The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainFree The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainKen Collins
 
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XML
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XMLFrontcon Riga - GraphQL Will Do To REST What JSON Did To XML
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XMLRoy Derks
 
Elixir - After 2 years in action + code WebUp
Elixir - After 2 years in action + code  WebUpElixir - After 2 years in action + code  WebUp
Elixir - After 2 years in action + code WebUpOliver Kriska
 
Keeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeKeeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeChristian Keuerleber
 
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...Vladimir Dejanovic
 
NodeJS Serverless backends for your frontends
NodeJS Serverless backends for your frontendsNodeJS Serverless backends for your frontends
NodeJS Serverless backends for your frontendsCarlos Santana
 
Atlanta MLconf Machine Learning Conference 09-23-2016
Atlanta MLconf Machine Learning Conference 09-23-2016Atlanta MLconf Machine Learning Conference 09-23-2016
Atlanta MLconf Machine Learning Conference 09-23-2016Chris Fregly
 
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016MLconf
 
CodePolitan Webinar: The Rise of PHP
CodePolitan Webinar: The Rise of PHPCodePolitan Webinar: The Rise of PHP
CodePolitan Webinar: The Rise of PHPSteeven Salim
 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TSGrigory Petrov
 

Similaire à A Modest Introduction To Swift (20)

A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to Swift
 
A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to Swift
 
A Modest Introduction To Swift
A Modest Introduction To SwiftA Modest Introduction To Swift
A Modest Introduction To Swift
 
A Modest Introduction to Swift
A Modest Introduction to SwiftA Modest Introduction to Swift
A Modest Introduction to Swift
 
Logs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to youLogs Are Magic! Why git workflows & commit structure should matter to you
Logs Are Magic! Why git workflows & commit structure should matter to you
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
#speakerlife
#speakerlife#speakerlife
#speakerlife
 
Server Side Swift
Server Side SwiftServer Side Swift
Server Side Swift
 
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...
Web development with Lua: Introducing Sailor an MVC web framework @ CodingSer...
 
Perl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code LinterPerl::Lint - Yet Another Perl Source Code Linter
Perl::Lint - Yet Another Perl Source Code Linter
 
Free The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own DomainFree The Enterprise With Ruby & Master Your Own Domain
Free The Enterprise With Ruby & Master Your Own Domain
 
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XML
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XMLFrontcon Riga - GraphQL Will Do To REST What JSON Did To XML
Frontcon Riga - GraphQL Will Do To REST What JSON Did To XML
 
Elixir - After 2 years in action + code WebUp
Elixir - After 2 years in action + code  WebUpElixir - After 2 years in action + code  WebUp
Elixir - After 2 years in action + code WebUp
 
Keeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decadeKeeping a codebase fresh for over a decade
Keeping a codebase fresh for over a decade
 
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
REST API vs gRPC, which one should you use in breaking a monolith [Dev conf 2...
 
NodeJS Serverless backends for your frontends
NodeJS Serverless backends for your frontendsNodeJS Serverless backends for your frontends
NodeJS Serverless backends for your frontends
 
Atlanta MLconf Machine Learning Conference 09-23-2016
Atlanta MLconf Machine Learning Conference 09-23-2016Atlanta MLconf Machine Learning Conference 09-23-2016
Atlanta MLconf Machine Learning Conference 09-23-2016
 
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
Chris Fregly, Research Scientist, PipelineIO at MLconf ATL 2016
 
CodePolitan Webinar: The Rise of PHP
CodePolitan Webinar: The Rise of PHPCodePolitan Webinar: The Rise of PHP
CodePolitan Webinar: The Rise of PHP
 
Migrating Web SDK from JS to TS
Migrating Web SDK from JS to TSMigrating Web SDK from JS to TS
Migrating Web SDK from JS to TS
 

Plus de John Anderson

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

Plus de John Anderson (20)

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

Dernier

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 

Dernier (20)

WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 

A Modest Introduction To Swift

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