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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Parveen Kaler
July 07, 2015
Programming
810
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
Build-to-own AI: Agentic Development for Humans
inesmontani
PRO
0
110
FDEが実現するAI駆動経営の現在地
gonta
2
230
AI がコードを書く時代における新卒エンジニアの仕事風景 (2026) / New Graduate Engineers in the Era of AI Coding (2026)
sushichan044
0
240
【やさしく解説 設計編 #0】DDDのコード、読めるのに分からない人へ
panda728
PRO
2
280
Claude Opus 4.6以後の受託開発エンジニアの変化(Claude Code開発ノウハウ大公開スペシャルbyクラスメソッド)
iidatakuma
1
890
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
550
数百円から始めるRuby電子工作
tarosay
0
110
광주소프트웨어마이스터고등학교 DevFest 특강 - 바이브 코딩 시대에서 주니어 개발자로 살아남는 방법
utilforever
1
160
2年かけて Deno に DOMMatrix を実装した話 / How I implemented DOMMatrix in Deno over two years
petamoriken
0
180
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
720
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
550
Go言語とトイモデルで学ぶTransformerの気持ち / fukuokago23-transformer
monochromegane
0
150
Featured
See All Featured
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
360
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
280
Fashionably flexible responsive web design (full day workshop)
malarkey
408
67k
Ethics towards AI in product and experience design
skipperchong
2
330
Navigating Team Friction
lara
192
16k
A Modern Web Designer's Workflow
chriscoyier
698
190k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
430
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
6k
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