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
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
過去最大のMCPアップデート! 2026-07-28 RC版の謎に迫る
licux
6
340
Agentic UI
manfredsteyer
PRO
0
160
Creating Composable Callables in Contemporary C++
rollbear
0
130
スマートグラスで並列バイブコーディング
hyshu
0
140
net-httpのHTTP/2対応について
naruse
0
490
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
640
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
130
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
5k
Go1.27で導入されるジェネリクスメソッドでできること
mackee
0
120
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.2k
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
280
Featured
See All Featured
A Soul's Torment
seathinner
6
2.9k
GitHub's CSS Performance
jonrohan
1033
470k
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
360
Why Our Code Smells
bkeepers
PRO
340
58k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Color Theory Basics | Prateek | Gurzu
gurzu
0
360
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1.1k
Raft: Consensus for Rubyists
vanstee
141
7.5k
Visualization
eitanlees
152
17k
Discover your Explorer Soul
emna__ayadi
2
1.1k
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