Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What's New in Swift 2.0
Search
Parveen Kaler
July 07, 2015
Programming
820
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
What's New in Swift 2.0
Parveen Kaler
July 07, 2015
More Decks by Parveen Kaler
See All by Parveen Kaler
Unit Testing UIViewController
kaler
0
400
Millions of Customers. Billions of Dollars: iOS Architecture at Scale
kaler
0
2.5k
Opinionated Core Data: Hold On To Your Butts
kaler
0
1.8k
Other Decks in Programming
See All in Programming
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
350
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.9k
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
170
KotlinConf Extended South Korea 2026 Keynote
l2hyunwoo
0
120
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
1.1k
GDG Korea Android: 2026 I/O Extended ~ What's new in Android development tools
pluu
0
240
TSX の <Hoge<Fuga>> という構文に驚いた話 / tsx-type-argument-syntax
kanaru0928
0
240
AIを紡ぐPMのお話
swdtkuy
0
120
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
200
PostgreSQL 18で考えるUUID主キー
kazuhiro1982
0
500
Built Our Own Background Agent at LayerX
layerx
PRO
10
5.9k
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
220
Featured
See All Featured
Amusing Abliteration
ianozsvald
1
260
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
360
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
250
Agile that works and the tools we love
rasmusluckow
331
22k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
A better future with KSS
kneath
240
18k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
490
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
270
The Mindset for Success: Future Career Progression
greggifford
PRO
0
470
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
11k
Balancing Empowerment & Direction
lara
6
1.2k
We Are The Robots
honzajavorek
0
300
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