What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John...
Transcript of What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John...
![Page 1: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/1.jpg)
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.
What’s New in Swift
Chris Lattner Sr. Director, Developer ToolsJohn McCall Compiler Engineer
Featured #WWDC15
Session 106
![Page 2: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/2.jpg)
Goals
![Page 3: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/3.jpg)
Goals
Refine language and tools fundamentals
![Page 4: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/4.jpg)
Goals
Refine language and tools fundamentalsAffordances for writing robust and safe code
![Page 5: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/5.jpg)
Goals
Refine language and tools fundamentalsAffordances for writing robust and safe codeEnable expressive libraries and APIs
![Page 6: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/6.jpg)
What’s New
Swift 2
![Page 7: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/7.jpg)
What’s New
FundamentalsPattern MatchingAvailability CheckingProtocol ExtensionsError Handling
Swift 2
![Page 8: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/8.jpg)
Fundamentals
![Page 9: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/9.jpg)
enum Animals { case Dog, Cat, Troll, Dragon }
Enums
let a = Animals.Dragon
![Page 10: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/10.jpg)
enum Animals { case Dog, Cat, Troll, Dragon }
Enums
let a = Animals.Dragon
print(a)
(Enum Value)
![Page 11: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/11.jpg)
enum Animals { case Dog, Cat, Troll, Dragon }
Enums
let a = Animals.Dragon
print(a)
Animals.Dragon
![Page 12: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/12.jpg)
Associated Values in Enums
![Page 13: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/13.jpg)
enum Either<T1, T2> { case First(T1) case Second(T2) }
Associated Values in Enums
![Page 14: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/14.jpg)
enum Either<T1, T2> { case First(T1) case Second(T2) }
Associated Values in Enums
Playground Console Output
Playground execution failed: /var/folders/3w/j_3ky_8966j30xj_2b9v6b0h0000gq/T/./lldb/2582/playground1.swift:28:6: error: unimplemented ir generation feature non-fixed multi-payload enum layout enum Either<T1, T2> { ^
![Page 15: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/15.jpg)
enum Either<T1, T2> { case First(T1) case Second(T2) }
Associated Values in Enums
![Page 16: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/16.jpg)
enum Tree<T> { case Leaf(T) }
case Node(Tree, Tree)
Recursive Enums
![Page 17: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/17.jpg)
enum Tree<T> { case Leaf(T) }
case Node(Tree, Tree)
Recursive Enums
Recursive value type 'Tree<T>' is not allowed!
![Page 18: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/18.jpg)
enum Tree<T> { case Leaf(T) }
case Node(Tree, Tree)
Recursive Enums
indirect
![Page 19: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/19.jpg)
“do” Statement
let a = Animals.Troll ...
![Page 20: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/20.jpg)
“do” Statement
let a = Animals.Troll ...
do {
}
![Page 21: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/21.jpg)
“do” Statement
let a = Animals.Troll ...
do { // // // // // lots of code here // //
do {
}
![Page 22: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/22.jpg)
“do” Statement
let a = Animals.Troll ...
do { // // // // // lots of code here // //
do {
}
repeat {
![Page 23: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/23.jpg)
Option Sets
![Page 24: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/24.jpg)
Option Sets
viewAnimationOptions = .Repeat | .CurveEaseIn | .TransitionCurlUpSwift 1:
![Page 25: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/25.jpg)
Option Sets
viewAnimationOptions = .Repeat | .CurveEaseIn | .TransitionCurlUp
viewAnimationOptions = nil
if viewAnimationOptions & .TransitionCurlUp != nil {
Swift 1:
![Page 26: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/26.jpg)
Option Sets
viewAnimationOptions = .Repeat | .CurveEaseIn | .TransitionCurlUp
viewAnimationOptions = nil
if viewAnimationOptions & .TransitionCurlUp != nil {
viewAnimationOptions = [.Repeat, .CurveEaseIn, .TransitionCurlUp]
Swift 2:
Swift 1:
![Page 27: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/27.jpg)
Option Sets
viewAnimationOptions = .Repeat | .CurveEaseIn | .TransitionCurlUp
viewAnimationOptions = nil
if viewAnimationOptions & .TransitionCurlUp != nil {
viewAnimationOptions = []
viewAnimationOptions = [.Repeat, .CurveEaseIn, .TransitionCurlUp]
Swift 2:
Swift 1:
![Page 28: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/28.jpg)
Option Sets
viewAnimationOptions = .Repeat | .CurveEaseIn | .TransitionCurlUp
viewAnimationOptions = nil
if viewAnimationOptions & .TransitionCurlUp != nil {
viewAnimationOptions = []
viewAnimationOptions = [.Repeat, .CurveEaseIn, .TransitionCurlUp]
if viewAnimationOptions.contains(.TransitionCurlUp) {
Swift 2:
Swift 1:
![Page 29: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/29.jpg)
Defining an Option Set
struct MyFontStyle : OptionSetType {
![Page 30: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/30.jpg)
Defining an Option Set
struct MyFontStyle : OptionSetType {let rawValue : Int
![Page 31: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/31.jpg)
Defining an Option Set
struct MyFontStyle : OptionSetType {let rawValue : Int
static let Bold = MyFontStyle(rawValue: 1) static let Italic = MyFontStyle(rawValue: 2) static let Underline = MyFontStyle(rawValue: 4) static let Strikethrough = MyFontStyle(rawValue: 8) }
![Page 32: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/32.jpg)
Defining an Option Set
struct MyFontStyle : OptionSetType {
myFont.style = [] myFont.style = [.Underline] myFont.style = [.Bold, .Italic]
if myFont.style.contains(.StrikeThrough) {
let rawValue : Int static let Bold = MyFontStyle(rawValue: 1) static let Italic = MyFontStyle(rawValue: 2) static let Underline = MyFontStyle(rawValue: 4) static let Strikethrough = MyFontStyle(rawValue: 8) }
Protocol-Oriented Programming in Swift Nob Hill Wednesday 2:30PM
![Page 33: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/33.jpg)
Functions and Methods
func save(name: String, encrypt: Bool) { … }
class Widget { func save(name: String, encrypt: Bool) { … }
![Page 34: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/34.jpg)
Functions and Methods
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
save("thing", false)
class Widget { func save(name: String, encrypt: Bool) { … }
![Page 35: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/35.jpg)
Functions and Methods
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
save("thing", false)
Swift 1 followed Objective-C:• No label for global functions• Labels for second argument (and later) in methods
class Widget { func save(name: String, encrypt: Bool) { … }
![Page 36: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/36.jpg)
save("thing",
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
class Widget { func save(name: String, encrypt: Bool) { … }
false)
Consistent Argument Labels
![Page 37: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/37.jpg)
save("thing",
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
class Widget { func save(name: String, encrypt: Bool) { … }
encrypt: false)
Consistent Argument Labels
![Page 38: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/38.jpg)
All ‘func’ declarations are uniform:• First argument is implied by the base function name• Labels are used for subsequent arguments
save("thing",
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
class Widget { func save(name: String, encrypt: Bool) { … }
encrypt: false)
Consistent Argument Labels
![Page 39: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/39.jpg)
All ‘func’ declarations are uniform:• First argument is implied by the base function name• Labels are used for subsequent arguments
save("thing",
func save(name: String, encrypt: Bool) { … }
widget.save("thing", encrypt: false)
class Widget { func save(name: String, encrypt: Bool) { … }
encrypt: false)
No change to imported C and Objective-C APIs
Consistent Argument Labels
![Page 40: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/40.jpg)
save("foo", encrypt: true)func save(name: String, encrypt: Bool)
Argument Label Controls
![Page 41: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/41.jpg)
save("foo", encrypt: true)func save( name: String, encrypt: Bool)_
Argument Label Controls
![Page 42: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/42.jpg)
save("foo", encrypt: true)func save( name: String, encrypt : Bool)_ encrypt
Argument Label Controls
![Page 43: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/43.jpg)
save("foo", encrypt: true)
save(name: "foo", encrypt: true)
Duplicate first name to enable argument label
func save(
func save(name name: String, encrypt: Bool)
name: String, encrypt : Bool)_ encrypt
Argument Label Controls
![Page 44: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/44.jpg)
save("foo", encrypt: true)
save(name: "foo", encrypt: true)
Duplicate first name to enable argument label
Underscore disables argument labels
func save(
save("foo", true)func save(name : String, _ encrypt : Bool)
func save(name name: String, encrypt: Bool)
name: String, encrypt : Bool)_ encrypt
Argument Label Controls
![Page 45: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/45.jpg)
save("foo", encrypt: true)
save(name: "foo", encrypt: true)
Duplicate first name to enable argument label
Underscore disables argument labels
func save(
save("foo", true)func save(name : String, _ encrypt : Bool)
func save(name name: String, encrypt: Bool)
name: String, encrypt : Bool)_ encrypt
Argument Label Controls
Much simpler model:• Consistency between functions and methods• No special rules for defaulted parameters• # argument syntax has been removed
![Page 46: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/46.jpg)
Diagnostics
![Page 47: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/47.jpg)
struct MyCoordinates { var points : [CGPoint]
points[42].x = 19
} }
func updatePoint() {
Diagnostics
![Page 48: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/48.jpg)
struct MyCoordinates { var points : [CGPoint]
points[42].x = 19
} }
func updatePoint() {
Diagnostics
^'@lvalue $T7' is not identical to 'CGFloat'!
![Page 49: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/49.jpg)
struct MyCoordinates { var points : [CGPoint]
points[42].x = 19
} }
func updatePoint() {
Diagnostics
^Cannot assign to the result of this expression!
![Page 50: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/50.jpg)
struct MyCoordinates { var points : [CGPoint]
points[42].x = 19
} }
func updatePoint() {
Diagnostics
^Cannot assign to ‘x’: ‘self’ is immutable!
![Page 51: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/51.jpg)
struct MyCoordinates { var points : [CGPoint]
points[42].x = 19
} }
func updatePoint() {
Diagnostics
^
mutating
Fix-it Mark method ‘mutating’ to make ‘self’ mutableCannot assign to ‘x’: ‘self’ is immutable
Cannot assign to ‘x’: ‘self’ is immutable!
![Page 52: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/52.jpg)
New Warnings
func processEntries() { var entries = ...
let size = entries.count
var indices = entries.map { $0.index } indices.sort()
![Page 53: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/53.jpg)
New Warnings
func processEntries() { var entries = ...
let size = entries.count
var indices = entries.map { $0.index } indices.sort()
Variable ‘entries’ was never mutated
![Page 54: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/54.jpg)
New Warnings
func processEntries() { var entries = ...
let size = entries.count
var indices = entries.map { $0.index } indices.sort()
Variable ‘entries’ was never mutated
Immutable value ‘size’ was never used
![Page 55: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/55.jpg)
New Warnings
func processEntries() { var entries = ...
let size = entries.count
var indices = entries.map { $0.index } indices.sort()
Variable ‘entries’ was never mutated
Immutable value ‘size’ was never used
‘sort’ result is unused; use ‘sortInPlace’ to mutate in-place
![Page 56: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/56.jpg)
SDK Improvements
class func requestHeaderFieldsWithCookies(cookies: [AnyObject] ) -> [NSObject : AnyObject]
!!
![Page 57: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/57.jpg)
SDK Improvements
class func requestHeaderFieldsWithCookies(cookies: [AnyObject] ) -> [NSObject : AnyObject]
![Page 58: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/58.jpg)
SDK Improvements
class func requestHeaderFieldsWithCookies(cookies: [AnyObject] ) -> [NSObject : AnyObject]
[NSHTTPCookie])[String : String]
Adoption of new features and best practices:• Nullability qualifiers• Objective-C typed collections• NS_ENUM, NS_OPTIONS, instancetype, @property, etc
Swift and Objective-C Interoperability Mission Tuesday 1:30PM
What’s New in Cocoa Presidio Tuesday 1:30PM
![Page 59: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/59.jpg)
Unit Testing
![Page 60: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/60.jpg)
Unit Testing and Access Control
![Page 61: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/61.jpg)
Unit Testing
Your code is now built in a “compile for testing” build mode:• Unit tests use
• public and internal symbols are now available
@testable import MyApp
and Access Control
![Page 62: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/62.jpg)
Unit Testing
What’s New in Xcode Presidio Tuesday 9:00AM
UI Testing in Xcode Nob Hill Wednesday 11:00AM
No behavior change for release builds:• Same optimizations • Same insurance against symbol collisions
Your code is now built in a “compile for testing” build mode:• Unit tests use
• public and internal symbols are now available
@testable import MyApp
and Access Control
![Page 63: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/63.jpg)
Rich Comments
![Page 64: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/64.jpg)
Rich Comments
![Page 65: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/65.jpg)
Rich Comments
Authoring Rich Playgrounds Presidio Wednesday 10:00AM
![Page 66: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/66.jpg)
![Page 67: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/67.jpg)
![Page 68: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/68.jpg)
![Page 69: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/69.jpg)
![Page 70: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/70.jpg)
So Much More…
![Page 71: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/71.jpg)
Default implementations in protocolsC function pointers + closuresRecursive nested functionsif/do labeled breaksSIMD VectorsMirrors APIreadLine()@nonobjc
So Much More…
![Page 72: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/72.jpg)
Default implementations in protocolsC function pointers + closuresRecursive nested functionsif/do labeled breaksSIMD VectorsMirrors APIreadLine()@nonobjc
The Swift Programming LanguageXcode 7 release notes
So Much More…
![Page 73: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/73.jpg)
Pattern Matching
![Page 74: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/74.jpg)
“if let” Statement
...
{
} }
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let dest = segue.destinationViewController as? BlogViewController
![Page 75: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/75.jpg)
“if let” Statement: the “Pyramid of Doom”
if { if {
} }
let blogIndex = tableView.indexPathForSelectedRow()?.rowsegue.identifier == blogSegueIdentifierdest.blogName = swiftBlogs[blogIndex] ... ...
{
} }
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let dest = segue.destinationViewController as? BlogViewController
![Page 76: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/76.jpg)
“if let” Statement: the “Pyramid of Doom”
if { if {
} }
let blogIndex = tableView.indexPathForSelectedRow()?.rowsegue.identifier == blogSegueIdentifierdest.blogName = swiftBlogs[blogIndex] ... ...
{
} }
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let dest = segue.destinationViewController as? BlogViewController
![Page 77: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/77.jpg)
“if let” Statement: Compound Conditions
where {let blogIndex = tableView.indexPathForSelectedRow()?.row
segue.identifier == blogSegueIdentifierdest.blogName = swiftBlogs[blogIndex] ... ...
} }
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let dest = segue.destinationViewController as? BlogViewController
![Page 78: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/78.jpg)
“if let” Statement: Compound Conditions
where {let blogIndex = tableView.indexPathForSelectedRow()?.row
segue.identifier == blogSegueIdentifierdest.blogName = swiftBlogs[blogIndex] ... ...
} }
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if let dest = segue.destinationViewController as? BlogViewController
![Page 79: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/79.jpg)
Patterns for Early Exits
func process(json: AnyObject) -> Either<Person,String> { let name: String = json["name"] as? String if name == nil { return .Second(”missing name”)
?
}
![Page 80: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/80.jpg)
Patterns for Early Exits
?
func process(json: AnyObject) -> Either<Person,String> { let name: String = json["name"] as? String if name == nil { return .Second(”missing name”)
?
}
let year: Int = json["year"] as? Int if year == nil { return .Second("missing year”)
}
![Page 81: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/81.jpg)
Patterns for Early Exits
let person = processPerson(name , year ) return .First(person)
! !
?
func process(json: AnyObject) -> Either<Person,String> { let name: String = json["name"] as? String if name == nil { return .Second(”missing name”)
?
}
}
let year: Int = json["year"] as? Int if year == nil { return .Second("missing year”)
}
![Page 82: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/82.jpg)
Patterns for Early Exits
let person = processPerson(name , year ) return .First(person)
! !
?
func process(json: AnyObject) -> Either<Person,String> { let name: String = json["name"] as? String if name == nil { return .Second(”missing name”)
?
}
}
let year: Int = json["year"] as? Int if year == nil { return .Second("missing year”)
}
![Page 83: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/83.jpg)
Patterns for Early Exits
let person = processPerson(name , year ) return .First(person)
!
!
func process(json: AnyObject) -> Either<Person,String> { let name: String = json["name"] as? String if name == nil { return .Second(”missing name”)
}
}
let year: Int = json["year"] as? Int if year == nil { return .Second("missing year”)
}
![Page 84: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/84.jpg)
“guard” Statement
guard let name = json["name"] as? String else { return .Second("missing name") }
![Page 85: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/85.jpg)
else { return .Second("missing name") } guard return .Second("missing year")
“guard” Statement
let year = json["year"] as? Int else
func process(json: AnyObject) -> Either<Person,String> { guard let name = json["name"] as? String
let person = processPerson(name, year) return .First(person)
}
}
{
![Page 86: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/86.jpg)
“guard” Statement: Compound Conditions
, return .Second(“bad input”)
let year = json["year"] as? Int else
}
}
let person = processPerson(name, year) return .First(person)
{
func process(json: AnyObject) -> Either<Person,String> { guard let name = json["name"] as? String
![Page 87: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/87.jpg)
Pattern Matching with Switch
switch bar() {case .MyEnumCase(let value): where value != 42:
![Page 88: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/88.jpg)
Pattern Matching with Switch
switch bar() {case .MyEnumCase(let value): where value != 42: doThing(value)
default: break }
![Page 89: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/89.jpg)
Pattern Matching with “if case”
switch bar() {case .MyEnumCase(let value): where value != 42: doThing(value)
default: break }
![Page 90: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/90.jpg)
Pattern Matching with “if case”
if case .MyEnumCase(let value) = bar() where value != 42 { doThing(value) }
switch bar() {case .MyEnumCase(let value): where value != 42: doThing(value)
default: break }
![Page 91: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/91.jpg)
“for … in” Filtering
for value in mySequence {
![Page 92: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/92.jpg)
“for … in” Filtering
for value in mySequence { if value != "" { doThing(value) } }
![Page 93: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/93.jpg)
“for … in” Filtering
for value in mySequence { if value != "" { doThing(value) } }
for value in mySequence where value != "" { doThing(value) }
![Page 94: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/94.jpg)
“for … in” Filtering
for value in mySequence { if value != "" { doThing(value) } }
for value in mySequence where value != "" { doThing(value) }
for case .MyEnumCase(let value) in enumValues { doThing(value) }
![Page 95: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/95.jpg)
Pattern Matching Summary
![Page 96: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/96.jpg)
Pattern Matching Summary
New “guard” statement for early exits
![Page 97: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/97.jpg)
Pattern Matching Summary
New “guard” statement for early exits
‘case’ uniformly supported in control flow statements- switch/case, if case, guard case, for-in case, while case
![Page 98: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/98.jpg)
Pattern Matching Summary
New “guard” statement for early exits
‘case’ uniformly supported in control flow statements- switch/case, if case, guard case, for-in case, while case
Other improvements- x? pattern for optionals- Improved exhaustiveness checker- “Unreachable case” warning
![Page 99: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/99.jpg)
API Availability Checking
John McCall Type Checker
![Page 100: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/100.jpg)
New APIs
extension NSButton { @available(OSX 10.10.3) var springLoaded: Bool }
![Page 101: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/101.jpg)
The Old Approach
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() { dropButton.springLoaded = true }
![Page 102: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/102.jpg)
The Old Approach
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() { dropButton.springLoaded = true }
-[NSButton setSpringLoaded:]: unrecognized selector sent to instance 0x100010AD0!
![Page 103: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/103.jpg)
extension NSButton { @available(OSX 10.10.3) var springLoaded: Bool }
The Old Approach
![Page 104: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/104.jpg)
extension NSButton { @available(OSX 10.10.3) var springLoaded: Bool }
The Old Approach
![Page 105: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/105.jpg)
The Old Approach
}
dropButton.springLoaded = true
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() {
![Page 106: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/106.jpg)
The Old Approach
dropButton.springLoaded = true
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() {") {
} }
if respondsToSelector("setSpringLoaded dropButton.
![Page 107: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/107.jpg)
The Old Approach
dropButton.springLoaded = true
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() {") {
} }
if respondsToSelector("setSpringLoaded dropButton.
![Page 108: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/108.jpg)
:
The Old Approach
dropButton.springLoaded = true
@IBOutlet var dropButton: NSButton!
") {
} }
if respondsToSelector("setSpringLoaded dropButton.
override func awakeFromNib() {
![Page 109: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/109.jpg)
:
The Old Approach
dropButton.springLoaded = true
@IBOutlet var dropButton: NSButton!
") {
} }
if respondsToSelector("setSpringLoaded dropButton.
override func awakeFromNib() {
![Page 110: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/110.jpg)
The Better Approach
}
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() { dropButton.springLoaded = true
![Page 111: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/111.jpg)
The Better Approach
}
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() { dropButton.springLoaded = true error: springLoaded is only available
on Mac OS X 10.10.3 or later!
![Page 112: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/112.jpg)
The Better Approach
if #available(OSX 10.10.3, *) {
} }
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() {
dropButton.springLoaded = true
![Page 113: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/113.jpg)
The Better Approach
if #available(OSX 10.10.3, *) {
} }
@IBOutlet var dropButton: NSButton!
override func awakeFromNib() {
dropButton.springLoaded = true
![Page 114: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/114.jpg)
Availability Checking
Take advantage of new capabilities without abandoning usersEasy, expressive checksStatically enforced by the language
Swift in Practice Presidio Thursday 2:30PM
![Page 115: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/115.jpg)
Protocol Extensions
![Page 116: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/116.jpg)
Extensions
extension Array { func countIf(match: Element -> Bool) -> Int {
}
var n = 0 for value in self { if match(value) { n++ } } return n }
func countIf<T : CollectionType>(collection: T, match: T.Generator.Element -> Bool) -> Int {
![Page 117: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/117.jpg)
Extensions
extension Array { func countIf(match: Element -> Bool) -> Int {
}
var n = 0 for value in self { if match(value) { n++ } } return n }
func countIf<T : CollectionType>(collection: T, match: T.Generator.Element -> Bool) -> Int {
![Page 118: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/118.jpg)
func countIf<T : CollectionType>(collection: T, match: T.Generator.Element -> Bool) -> Int {
Extensions vs. Global Functions
var n = 0 for value in self { if match(value) { n++ } } return n }
extension Array { func countIf(match: Element -> Bool) -> Int {
{
![Page 119: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/119.jpg)
var n = 0 for value in self { if match(value) { n++ } } return n }
Extensions
extension Array func countIf(match: Element -> Bool) -> Int {
}
{
![Page 120: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/120.jpg)
extension Array func countIf(match: Element -> Bool) -> Int {
Protocol Extensions
var n = 0 for value in self { if match(value) { n++ } } return n }
}
{
![Page 121: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/121.jpg)
extension Array func countIf(match: Element -> Bool) -> Int {
Protocol Extensions
var n = 0 for value in self { if match(value) { n++ } } return n }
}
{CollectionType
![Page 122: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/122.jpg)
Methodification
let x = filter(map(numbers) { $0 * 3 }) { $0 >= 0 }
Global generic algorithms are becoming methods
Swift 1:
![Page 123: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/123.jpg)
Methodification
let x = numbers.map { $0 * 3 }.filter { $0 >= 0 }
let x = filter(map(numbers) { $0 * 3 }) { $0 >= 0 }
Global generic algorithms are becoming methods
Swift 1:
Swift 2:
![Page 124: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/124.jpg)
More consistent use of methods in the standard library
Powerful new design patterns
Protocol Extensions
Protocol-Oriented Programming in Swift Nob Hill Wednesday 2:30PM
![Page 125: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/125.jpg)
Error Handling
![Page 126: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/126.jpg)
Errors, Errors Everywhere
A big opportunity to improve programming in Swift• Safety• Readability• Expressiveness
![Page 127: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/127.jpg)
Errors, Errors Everywhere
Common problem, but the standard solutions aren't good, in different ways• Too easy to ignore errors• Repetitive, bug-prone patterns• Hard to reason about implicit behavior
![Page 128: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/128.jpg)
Kinds of Error
?Trivial errors• Int(someString) can fail, but an optional return type is fine
![Page 129: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/129.jpg)
Kinds of Error
! ?Trivial errors
• Int(someString)
Logic errors• Assertions, overflows, NSException, etc.
![Page 130: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/130.jpg)
Kinds of Error
!
?Detailed, recoverable errors• File not found• Network failure• User cancellation
Trivial errors• Int(someString)
Logic errors• Assertions, overflows, NSException, etc.
![Page 131: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/131.jpg)
Error Handling
func preflight( url.checkResourceIsReachable() resetState() }
) {
![Page 132: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/132.jpg)
Error Handling
func preflight( url.checkResourceIsReachable() resetState() }
) {
![Page 133: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/133.jpg)
Error Handling with NSError
func preflight(inout error: NSError? -> Bool if url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
) {
![Page 134: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/134.jpg)
Error Handling with NSError
func preflight(inout error: NSError? -> Bool if url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
) {
![Page 135: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/135.jpg)
Downsides
Adds a lot of boilerplateOriginal code is hiding in there somewhere
func preflight(inout error: NSError?) -> Bool { if url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
![Page 136: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/136.jpg)
func preflight(inout error: NSError?) -> Bool { if url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
Adds a lot of boilerplateOriginal code is hiding in there somewhereManually implementing conventions without help
Downsides
![Page 137: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/137.jpg)
func preflight(inout error: NSError?) -> Bool { if return false } resetState() return true }
Adds a lot of boilerplateOriginal code is hiding in there somewhereManually implementing conventions without help
url.checkResourceIsReachableAndReturnError(&error) {
Downsides
![Page 138: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/138.jpg)
func preflight(inout error: NSError?) -> Bool { if return false } resetState() return true }
Adds a lot of boilerplateOriginal code is hiding in there somewhereManually implementing conventions without help
url.checkResourceIsReachableAndReturnError(&error) {!
Downsides
![Page 139: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/139.jpg)
Upsides
Obvious that checkResourceIsReachable can fail
func preflight(inout error: NSError?) -> Bool { if !url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
![Page 140: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/140.jpg)
Obvious that checkResourceIsReachable can failObvious that preflight can fail
Upsides
func preflight(inout error: NSError?) -> Bool { if !url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
![Page 141: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/141.jpg)
Obvious that checkResourceIsReachable can failObvious that preflight can fail
Upsides
func preflight(inout error: NSError?) -> Bool { if !url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
![Page 142: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/142.jpg)
Obvious that checkResourceIsReachable can failObvious that preflight can failAll the control flow is explicit
Upsides
func preflight(inout error: NSError?) -> Bool { if !url.checkResourceIsReachableAndReturnError(&error) { return false } resetState() return true }
![Page 143: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/143.jpg)
resetState()
Error Handling in Swift
func preflight() { url.checkResourceIsReachable()
}
![Page 144: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/144.jpg)
resetState()
Error Handling in Swift
func preflight() { url.checkResourceIsReachable()
}
error: call can throw, but it is not marked with 'try' and the error is not handled!
![Page 145: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/145.jpg)
resetState()
try
func preflight() { url.checkResourceIsReachable()
}
try
![Page 146: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/146.jpg)
resetState()
try
func preflight() { url.checkResourceIsReachable()
}
tryerror: call can throw, but the error is not handled!
![Page 147: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/147.jpg)
func preflight()
resetState() }
try url.checkResourceIsReachable()
Propagating Errors
throws {
![Page 148: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/148.jpg)
-> Bool { do { try url.checkResourceIsReachable() resetState() return true } catch { return false
Handling Errors
func preflight()
} }
![Page 149: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/149.jpg)
-> Bool { do { try url.checkResourceIsReachable() resetState() return true } catch let error { return false
Patterns with “catch”
func preflight()
} }
![Page 150: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/150.jpg)
-> Bool { do { try url.checkResourceIsReachable() resetState() return true } catch NSURLError.FileDoesNotExist { return true } catch let error { return false
Patterns with “catch”
func preflight()
} }
![Page 151: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/151.jpg)
do { try resetState() } catch { fatalError("I sure didn't expect this error: \(error)") }
func preflight() {
url.checkResourceIsReachable()
}
Aside: Impossible Errors
![Page 152: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/152.jpg)
try! resetState()
func preflight() {url.checkResourceIsReachable()
}
Aside: Impossible Errors
![Page 153: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/153.jpg)
Error Types
ErrorType is a protocol in the Swift standard library
Any type that conforms to ErrorType can be thrown and caught• NSError already conforms to ErrorType• Can make your own types conform as well
![Page 154: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/154.jpg)
Enums as Error Types
enum is great for groups of related errors• Can carry data in a payload• Compiler handles protocol conformance details automatically
![Page 155: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/155.jpg)
Defining Your Own Error Type
enum DataError : ErrorType { case MissingName case MissingYear // add more later }
![Page 156: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/156.jpg)
Defining Your Own Error Type
func processPerson(json: AnyObject) -> Either<Person,String> { guard let name = json["name"] as? String { return .Second("missing name") }
guard let year = json["year"] as? Int { return .Second("missing year") }
return .First(Person(name, year)) }
![Page 157: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/157.jpg)
func processPerson(json: AnyObject) -> Either<Person,String> { guard let name = json["name"] as? String { throw DataError.MissingName }
guard let year = json["year"] as? Int { throw DataError.MissingYear }
return .First(Person(name, year)) }
Defining Your Own Error Type
![Page 158: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/158.jpg)
Defining Your Own Error Type
func processPerson(json: AnyObject) throws -> Person { guard let name = json["name"] as? String { throw DataError.MissingName }
guard let year = json["year"] as? Int { throw DataError.MissingYear }
return Person(name, year) }
![Page 159: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/159.jpg)
Cleaning Up
func processSale(json: AnyObject) throws { guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
return Sale(buyer, price) }
![Page 160: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/160.jpg)
func processSale(json: AnyObject) throws { guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
return Sale(buyer, price) }
Notifying at the Start
![Page 161: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/161.jpg)
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
return Sale(buyer, price) }
Notifying at the Start
![Page 162: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/162.jpg)
return Sale(buyer, price) }
Notifying at the End
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
![Page 163: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/163.jpg)
return Sale(buyer, price) }
delegate?.didEndReadingSale()
Notifying at the End
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
![Page 164: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/164.jpg)
Notifying at the End
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice } return Sale(buyer, price) }
delegate?.didEndReadingSale()
![Page 165: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/165.jpg)
Redundancy
throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int {
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int { throw DataError.MissingPrice } delegate?.didEndReadingSale() return Sale(buyer, price) }
![Page 166: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/166.jpg)
Redundancy
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary {
throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int {
throw DataError.MissingPrice }
delegate?.didEndReadingSale() return Sale(buyer, price) }
![Page 167: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/167.jpg)
Redundancy
delegate?.didEndReadingSale()
delegate?.didEndReadingSale()
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary {
throw DataError.MissingBuyer } let buyer = try processPerson(buyerJSON)
guard let price = json["price"] as? Int {
throw DataError.MissingPrice }
delegate?.didEndReadingSale() return Sale(buyer, price) }
![Page 168: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/168.jpg)
Redundancy and Awkwardness
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { delegate?.didEndReadingSale() throw DataError.MissingBuyer } let buyer
guard let price = json["price"] as? Int { delegate?.didEndReadingSale() throw DataError.MissingPrice }
delegate?.didEndReadingSale() return Sale(buyer, price) }
![Page 169: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/169.jpg)
Redundancy and Awkwardness
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale() guard let buyerJSON = json["buyer"] as? NSDictionary { delegate?.didEndReadingSale() throw DataError.MissingBuyer } let buyer : Person do { buyer = try processPerson(buyerJSON) } catch { delegate?.didEndReadingSale() throw error } guard let price = json["price"] as? Int { delegate?.didEndReadingSale() throw DataError.MissingPrice
![Page 170: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/170.jpg)
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale()
throw DataError.MissingBuyer } let buyer
Defer Actions
guard let buyerJSON = json["buyer"] as? NSDictionary {
guard let price = json["price"] as? Int {
throw DataError.MissingPrice }
![Page 171: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/171.jpg)
func processSale(json: AnyObject) throws { delegate?.didBeginReadingSale()
throw DataError.MissingBuyer } let buyer
defer { delegate?.didEndReadingSale() }
= try processPerson(buyerJSON)
return Sale(buyer, price) }
Defer Actions
guard let buyerJSON = json["buyer"] as? NSDictionary {
guard let price = json["price"] as? Int { throw DataError.MissingPrice }
![Page 172: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/172.jpg)
A Quick Note About Implementation
![Page 173: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/173.jpg)
A Quick Note About Implementation
"Exception handling" schemes• Anything can throw• Performance heavily biased against exceptions
![Page 174: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/174.jpg)
A Quick Note About Implementation
"Exception handling" schemes• Anything can throw• Performance heavily biased against exceptions
Swift error handling is more balanced• Not table-based unwinding• Similar in performance to an explicit “if” statement
![Page 175: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/175.jpg)
Swift 1: extension NSIncrementalStore { func loadMetadata(error: NSErrorPointer) -> Bool }
Works Great with Cocoa
![Page 176: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/176.jpg)
Swift 1: extension NSIncrementalStore { func loadMetadata(error: NSErrorPointer) -> Bool }
Swift 2: extension NSIncrementalStore { func loadMetadata() throws }
Works Great with Cocoa
![Page 177: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/177.jpg)
Swift 1: extension NSData { class func bookmarkDataWithContentsOfURL(bookmarkFileURL: NSURL, error: NSErrorPointer) -> NSData? }
Works Great with Cocoa
![Page 178: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/178.jpg)
Swift 1: extension NSData { class func bookmarkDataWithContentsOfURL(bookmarkFileURL: NSURL, error: NSErrorPointer) -> NSData? }
Swift 2: extension NSData { class func bookmarkDataWithContentsOfURL(bookmarkFileURL: NSURL) throws -> NSData }
Works Great with Cocoa
![Page 179: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/179.jpg)
Error Handling
Expressive way to handle errorsSafe, reliable programming modelReadable and maintainable
![Page 180: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/180.jpg)
Swift 2
What’s New in Swift 2
![Page 181: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/181.jpg)
FundamentalsPattern MatchingAvailability CheckingProtocol ExtensionsError Handling
Swift 2
What’s New in Swift 2
![Page 182: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/182.jpg)
More Information
Swift Language Documentationhttp://developer.apple.com/swift
Apple Developer Forumshttp://developer.apple.com/forums
Stefan LesserSwift [email protected]
![Page 183: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/183.jpg)
Related Sessions
Swift and Objective-C Interoperability Mission Tuesday 1:30PM
Improving Your Existing Apps with Swift Pacific Heights Tuesday 3:30PM
Authoring Rich Playgrounds Presidio Wednesday 10:00AM
Protocol-Oriented Programming in Swift Nob Hill Wednesday 2:30PM
Optimizing Swift Performance Presidio Thursday 9:00AM
Swift in Practice Presidio Thursday 2:30PM
Building Better Apps with Value Types in Swift Mission Friday 2:30PM
![Page 184: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/184.jpg)
Labs
Swift Lab Developer Tools Lab A Tuesday 1:30PM
Swift Lab Developer Tools Lab A Wednesday 9:00AM
Swift Lab Developer Tools Lab A Wednesday 1:30PM
Swift Lab Developer Tools Lab A Thursday 9:00AM
Swift Lab Developer Tools Lab A Thursday 1:30PM
Swift Lab Developer Tools Lab A Friday 9:00AM
Swift Lab Developer Tools Lab A Friday 1:30PM
![Page 185: What’s New in Swift · What’s New in Swift Chris Lattner Sr. Director, Developer Tools John McCall Compiler Engineer Featured #WWDC15 Session 106. Goals. Goals Refine language](https://reader033.fdocuments.us/reader033/viewer/2022041414/5e1a8d96f14e27661c4056c5/html5/thumbnails/185.jpg)