Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday...

83
© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Improving your Existing Apps with Swift Getting Swifty with It Woody L. in the Sea of Swift Developer Tools #WWDC15 Session 403

Transcript of Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday...

Page 1: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

© 2015 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Improving your Existing Apps with SwiftGetting Swifty with It

Woody L. 🐳 in the Sea of Swift

Developer Tools #WWDC15

Session 403

Page 2: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoSet the Time Machine to 2012

The Elements has been restored from a vault

Page 3: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Agenda

The “before” appModernizing UIMix and matchSwift StructsAvailabilityLive filtering

Page 4: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Modernizing UI

Walk. Walk. Fashion, Baby.

Page 5: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Tile Sizes

Small Tile Detail Tile

Page 6: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 7: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 8: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Pre-Rendered Backgrounds

256px

37px

Page 9: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Pre-Rendered Backgrounds

37px

256px

Page 10: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

256px = 256pt @ 1x 512px = 256pt @ 2x 768px = 256pt @ 3x

Page 11: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

256px = 256pt @ 1x 512px = 256pt @ 2x 768px = 256pt @ 3x

Does Not Exist

Does Not Exist

Page 12: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented
Page 13: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented
Page 14: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

ArgonAtomic Weight: 39.95

State: GasPeriod: 3Group: 18

Page 15: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

ArgonAtomic Weight: 39.95

State: GasPeriod: 3Group: 18

Page 16: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 17: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Tile Appearance

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 18: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Tile Appearance

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 19: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Tile Appearance

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 20: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Benefits of Custom Drawing Code

Goals Custom Drawing Code

Modernize the Look

Crisp, Sharp Corners

Resolution Independence

Page 21: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Mix and MatchExtending Objective-C Classes with Swift

An interoperability overture in the key of Swift.

Page 22: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class

Class Definition

Base

Page 23: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class + Category

Class Definition

Base Category

Page 24: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class + Categories

Class Definition

Base Category Category Category Category Category

Page 25: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class + Categories

Class Definition

Category Category Category Category CategoryBase

Page 26: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class + Categories + Swift Extensions

Class Definition

Extension Category Category Category CategoryBase

Page 27: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignBase Class + Categories

Class Definition

Base Extension Category CategoryExtension Extension

Page 28: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignMixed-Language Classes

Bridging Header

Generated Header

Base Extension

Page 29: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Class DesignMixed-Language Classes

Generated Header

Created by Compiler

Maintained by Compiler

#import “Product-Swift.h” into MyClass.m

Exists in DerrivedData

Bridging Header

Created by Xcode

Maintained by you

Contains #import “MyClass.h”

Exists in File Navigator

Page 30: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoModernizing The Elements UI

Boasting 120% daily intake of vitamin Swift.

Page 31: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Partially Rounded Corners

Page 32: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Partially Rounded Corners

Page 33: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Swift StructsWith Playground prototyping

Global utility functions: shake ‘em off.

Page 34: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Bezier Path

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 35: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Bounding Rectangle

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 36: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Draw StrokeStroke’s midpoint is the Bezier Path

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 37: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Draw StrokeStroke’s midpoint is the Bezier Path

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 38: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Draw StrokeClipping

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 39: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Draw StrokeClipping

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 40: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

SolutionInset the Bezier Path

18 ArArgon

Atomic Weight: 39.95State: GasPeriod: 3Group: 18

Discovered: 1849 A.D.

Page 41: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Utility FunctionsCore Graphics API

Global Utility FunctionsStruct

CGRect

CGRectZero() CGRectMake()

CGRectGetMaxY() CGRectGetWidth()

CGRectUnion() CGRectInset()

Page 42: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Utility FunctionsCore Graphics API

CGRect

CGRectZero() CGRectMake()

CGRectGetMaxY() CGRectGetWidth()

CGRectUnion() CGRectInset()

MethodsStruct

Page 43: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Utility FunctionsCore Graphics API

.zeroRect (x:,y:,w:,h:)

.maxY

.width .union(r)

.rectByInsetting(dx:,dy:)

CGRect

Initializers + MethodsStruct

Page 44: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Utility FunctionsCore Graphics API

Consistent

Initializer Syntax

Encapsulation

Code Completion

API Discovery

.zeroRect (x:,y:,w:,h:)

.maxY

.width .union(r)

.rectByInsetting(dx:,dy:)

CGRect

Initializers + MethodsStruct

Page 45: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Playgrounds for PrototypingTweak-build loop

Build / Compile

Copy into Simulator

Navigate to UI

Check Work

Commit Tweak Code

Page 46: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Playgrounds for PrototypingTweak-build loop

Build / Compile

Copy into Simulator

Navigate to UI

Check Work

Commit Tweak Code

Risk of DestabilizingKnown-Good Code

Slow

Page 47: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Playgrounds for PrototypingThe plan

Check Work

Tweak Code• Reduce Roundtrip Time• Iterative + Experimental Development

Reduce roundtrip timeIterative + experimental development

Page 48: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoStruct Methods

With Playground Prototyping

Page 49: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Availability: the Next Generation

Put down your if respondsToSelector() and step away from Xcode.

Page 50: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Runtime InterrogationThe classic way

if ([UIImagePickerController instancesRespondToSelector: @selector(availableCaptureModesForCameraDevice:)]) { // Method is available for use. } else { // Method is not available. }

Page 51: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Availability Based on OS VersionThe officially endorsed, modern way

if #available(iOS 8.3, *) { viewController.modalPresentationStyle = .Popover if let popoverPC = viewController.popoverPresentationController { let cell = tableView.cellForRowAtIndexPath(indexPath) popoverPC.sourceView = cell popoverPC.delegate = self } presentViewController(viewController, animated: true, completion: nil) } else { // Earlier than iOS 8.3 APIs navigationController?.pushViewController(viewController, animated: true) }

Page 52: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Compile-Time Availability Checking

viewController.modalPresentationStyle = .Popover if let popoverPC = viewController.popoverPresentationController { let cell = tableView.cellForRowAtIndexPath(indexPath) popoverPC.sourceView = cell popoverPC.delegate = self }

Page 53: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Compile-Time Availability Checking

viewController.modalPresentationStyle = .Popover if let popoverPC = viewController.popoverPresentationController { let cell = tableView.cellForRowAtIndexPath(indexPath) popoverPC.sourceView = cell popoverPC.delegate = self }

error: ‘popoverPresentationController’ is only available on iOS 8.0 or newer

Page 54: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoAvailability

APIs must be this tall to ride.

Page 55: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Availability

New in Swift 2Compilation-time checking of API AvailabilityRuntime checks inserted automatically

Swift in Practice Presidio Thursday 2:30PM

Page 56: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Live Search

Filtering elements since antiquity.

Page 57: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FeIRONNa

SODIUMCs

CESIUMNiNICKELCuCOPPER

ContentView Controller

TableView

Page 58: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FeIRON

NaSODIUM

CsCESIUM

NiNICKEL

CuCOPPER

TableView

Content

View Controller<UITableViewDataSource>

Page 59: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

TableView

Content FeIRON

NaSODIUM

CsCESIUM

NiNICKEL

CuCOPPER

View Controller<UITableViewDataSource, UISearchBarDelegate>

Page 60: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

TableView

Content FeIRON

NaSODIUM

CsCESIUM

NiNICKEL

CuCOPPER

View Controller<UITableViewDataSource, UISearchBarDelegate>

searchbar:textDidChange:

Page 61: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FiltersearchBar:textDidChange:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { if searchText.isEmpty {

content = atomicElements } else { content = atomicElements.filter{$0.name.hasPrefix(searchText)} } tableView?.reload() }

Page 62: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FiltersearchBar:textDidChange:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { if searchText.isEmpty {

content = atomicElements } else { content = atomicElements.filter{$0.name.hasPrefix(searchText)} } tableView?.reload() }

Page 63: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FiltersearchBar:textDidChange:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { if searchText.isEmpty == true {

content = atomicElements } else { content = atomicElements.filter{$0.name.hasPrefix(searchText)} } tableView?.reload() }

Original Array:

Filtered Array:

Closure:

Page 64: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

FiltersearchBar:textDidChange:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { if searchText.isEmpty == true {

content = atomicElements } else { content = atomicElements.filter{$0.name.hasPrefix(searchText)} } tableView?.reload() }

Original Array:

Filtered Array:

Closure:

Page 65: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoFiltering

Only the worthy may pass.

Page 66: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Sums of Atomic Weights

Introducing Map & Reduce, in harmony like Ebony & Ivory.

Page 67: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Sum of Atomic Weights

Page 68: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Sum of Atomic WeightsTwo or more rows selected

Page 69: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Intermediate ObjectiveArray of selected atomic elements

F9

Eu63

Es99

Dy66

Cu29

Db105

Er68

content

Page 70: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Intermediate ObjectiveArray of selected atomic elements

F9

Eu63

Es99

Dy66

Cu29

Db105

Er68

content

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

Page 71: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Intermediate ObjectiveArray of selected atomic elements

2526272829303132

222324

indexPath.row

F9

Eu63

Es99

Dy66

Cu29

Db105

Er68

content

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

Page 72: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Intermediate ObjectiveArray of selected atomic elements

25 27 29 30

indexPathsForSelectedRows

31

F9

Eu63

Es99

Dy66

Cu29

Db105

Er68

content

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

Page 73: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Traditional ApproachFor in

var selectedElements = [AtomicElement]() if let indexPaths = tv?.indexPathsForSelectedRows { for ip in indexPaths { let currentElement = content[ip.row] selectedElements.append(currentElement) } }

Page 74: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Swiftier WayMap

let selectedElements = tv?.indexPathsForSelectedRows?.map{content[$0.row]}

var selectedElements = [AtomicElement]() if let indexPaths = tv?.indexPathsForSelectedRows { for ip in indexPaths { let currentElement = content[ip.row] selectedElements.append(currentElement) } }

Page 75: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Adding Values of ItemsTraditional way

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

.atomicWeight

Page 76: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Adding Values of ItemsTraditional way

Using a for-in loop

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

.atomicWeight

var d = 0.0 for element in selectedAtomicElements { d = d + element.atomicWeight }

Page 77: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Sum of Atomic Element WeightsSwift’s reduce() method

reduce(0.0, combine: {$0 + $1.atomicWeight})With Reduce()

var d = 0.0 for element in selectedAtomicElements { d = d + element.atomicWeight }

Using a for-in loop

F9

Eu63

Es99

Dy66

Cu29

selectedAtomicElements

.atomicWeight

Page 78: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Final Code SnippetAll together, now

tableView?.indexPathsForSelectedRows?.map{self.content[$0.row]}.reduce(0.0, combine: {$0 + $1.atomicWeight})

Page 79: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

DemoMap & Reduce

Obtaining closure through closures.

Page 80: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Summary

You have existing Objective-C code? Keep it.You’re adding new code? Consider writing it in Swift.

Page 81: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

More Information

Stefan LesserSwift [email protected]

Swift Language Documentationhttp://developer.apple.com/swift

Apple Developer Forumsdeveloper.apple.com/forums

Page 82: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented

Related Sessions

What’s new in Swift Presidio Tuesday 11:10AM

Swift and Objective-C Interoperability Mission Tuesday 1:30PM

Protocol Oriented Programming in Swift Marina Wednesday 2:30PM

Optimizing Performance in Swift Presidio Thursday 9:00AM

Swift in Practice Presidio Thursday 2:30PM

Building Better UIKit Apps with Swift Mission Wednesday 2:00PM

Page 83: Improving your Existing Apps with Swift - Apple Inc. · What’s new in Swift Presidio Tuesday 11:10AM Swift and Objective-C Interoperability Mission Tuesday 1:30PM Protocol Oriented