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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Parveen Kaler
July 07, 2015
Programming
800
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
390
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.7k
Other Decks in Programming
See All in Programming
Contextとはなにか
chiroruxx
1
330
TSKaigi Night Talks 2026_TypeScriptでサプライチェーンの整合性を型に閉じ込める
geekplus_tech
0
350
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
Inside Stream API
skrb
1
720
The NotImplementedError Problem in Ruby
koic
1
800
Datadog × OpenTelemetry 入門と実践のあいだ
kn_to_maxpno
1
160
Developing with AI Agents — Codex, Claude Code & Cowork Practical Guide
x5gtrn
PRO
0
1.3k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.4k
CSC307 Lecture 17
javiergs
PRO
0
320
技術記事、 専門家としてのプログラマ、 言語化
mizchi
13
6k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
120
dRuby over BLE
makicamel
2
340
Featured
See All Featured
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
600
Google's AI Overviews - The New Search
badams
0
1k
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
390
Java REST API Framework Comparison - PWX 2021
mraible
34
9.4k
KATA
mclloyd
PRO
35
15k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
470
Documentation Writing (for coders)
carmenintech
77
5.4k
30 Presentation Tips
portentint
PRO
1
320
So, you think you're a good person
axbom
PRO
2
2.1k
Designing Powerful Visuals for Engaging Learning
tmiket
1
410
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
170
How to train your dragon (web standard)
notwaldorf
97
6.7k
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