Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up
for free
What's New in Swift 2.0
Parveen Kaler
July 07, 2015
Programming
0
580
What's New in Swift 2.0
Parveen Kaler
July 07, 2015
Tweet
Share
More Decks by Parveen Kaler
See All by Parveen Kaler
kaler
0
220
kaler
0
2.2k
kaler
0
890
Other Decks in Programming
See All in Programming
fuga0618
1
150
rshindo
2
310
azdaroth
0
140
horie1024
1
430
nbkouhou
1
1.3k
mfunaki
1
300
muttsu_623
0
550
showwin
0
130
masayaaoyama
4
830
line_developers_tw
1
500
ogidow
0
170
manfredsteyer
PRO
0
280
Featured
See All Featured
bkeepers
52
4.1k
malarkey
393
60k
andyhume
62
3.5k
zenorocha
297
40k
mongodb
23
3.8k
paulrobertlloyd
71
3.6k
yeseniaperezcruz
302
31k
dougneiner
119
7.8k
jlugia
216
16k
iamctodd
17
1.9k
imathis
478
150k
cassininazir
347
20k
Transcript
What's New in Swift 2.0 © Smartful Studios, 2015 •
http://parveenkaler.com • @kaler
What's New in Swift 2.0 1. Protocols 2. Error handling
3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What's New in Swift 2.0 1. Protocols 2. Error handling
3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What is a Protocol? The Basics protocol SomeType { var
someVar: Int { get set } static var someTypeVar: Int func someFunc() static func someTypeFunc() } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What is a Protocol? Even More • mutating keyword •
init, required init • @objc and optional © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What's New with Protocols Protocol Extensions allow protocols to contain
method implementations, not just declarations. © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Protocol Extenstion with Type Constraint extension CollectionType where Self.Generator.Element: IntegerArithmeticType
{ var average: Self.Generator.Element { var accumulate = 0 var count = 0 for c in self { accumulate += c count++ } return count == 0 ? 0 : accumulate/count } } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Extensions & Dynamic Dispatch protocol P { func this() }
extension P { func this() { print("This extension") } func that() { print("That extension") } } struct S: P { func this() { print("This struct") } func that() { print("That struct") } } let s = S() s.this() // "This struct" s.that() // "That extension" © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What's New in Swift 2.0 1. Protocols 2. Error handling
3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Swift 2.0 Error Model • Looks like exceptions but it's
actually return values • Forces checking of return value © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Old Cocoa Error Handling NSError *error; BOOL success = [object
functionMayFailWithError:&error]; if (success == NO) { // Check the error variable } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Swift 2.0 Error Handling do { try object.firstFunctionThatMayFail() // the
try is mandatory try object.secondFunctionThatMayFail() } catch { } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Representing Errors Conform to the empty ErrorType protocol. enum HTTPError
: ErrorType { case BadRequest = 400 case Unauthorized = 401 case Forbidden = 403 case NotFound = 404 } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
"Throwing" Errors struct WeirdError : ErrorType { let error: String
} func functionThatMayThrow() throws -> Int { if someStateIsWeird { throw WeirdError } return 0 } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
"Catching" Errors do { try functionThatMayThrow() } catch WeirdError {
print("Weird Error") } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
"Catching" Errors • catch patterns are like switch • must
be exhaustive or function must be marked as throws to propogate • an empty pattern catches all errors © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
Force Try try! functionThatMayThrow() • disables error propagation • Warning:
runtime assertion if an exception is thrown © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What's New in Swift 2.0 1. Protocols 2. Error handling
3. Control flow © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
defer func readFile(filename: String) throws { let file = open(filename)
defer { close(file) } while let line = readline(file) { ... } // defer is executed here } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
defer • if there are multiple defer blocks in a
scope, they are executed in reverse order • defer blocks CAN NOT return or break © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
guard func printName(name: String?) { guard let name = name
else { return } print("Hello \(name)") } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
guard • optional bindings are available for the rest of
the code block • guard MUST call return, break, or continue © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
guard & defer Like chocolate and peanut butter func readFile(filename:
String) { guard let file = open(filename) else { return } defer { close(file) } while let line = readline(file) { ... } } © Smartful Studios, 2015 • http://parveenkaler.com • @kaler
What's New in Swift 2.0 • @kaler - http://parveenkaler.com •
@smartful - http://smartfulstudios.com • @SwiftNewsCo - http://swiftnews.co © Smartful Studios, 2015 • http://parveenkaler.com • @kaler