Tag Archives: watchOS

WWDC 2016

I had the opportunity to go to my 3rd WWDC (Apple’s World Wide Developers Conference).  WWDC is a great place to learn about the new things Apple is introducing in upcoming versions of their products as well as to talk with Apple engineers and others who are developing for their platforms.

All of the content from the sessions delivered at WWDC is available online.

High-Level Summary

  • New features in iOS 10
    • SiriKit
    • New user notification framework with more ways to display and manage notifications
    • New extension points in iMessage
    • Improvements in UIKit animations and transitions
    • Improvements in collection views and table views
  • New features in watchOS 3
    • Button modified to bring up a dock of active apps.  Glances removed in favor of docked view of app.
    • Background app refresh
    • Changes to navigation by using the digital crown to navigate between elements at the same level.
    • Access to the digital crown.
  • Swift 3
    • The API design guidelines were applied to the language and Cocoa/CocoaTouch frameworks.
  • Networking
    • HTTP/2 Server Push is now supported
    • Network statistics

Evaluation

The changes in the notification system and in watchOS can really improve the user experience for presenting notifications.

The changes to Swift continue to improve the safety provided by the language to reduce the occurrence of bugs.

Good info on improving the performance of table and collection views.

HTTP/2 Server Push can significantly improve the responsiveness of mobile apps.

Apple made some really nice changes in watchOS (notifications, background refresh, navigation) that should make the watch a lot easier to use.

Sessions

What's New in Swift

Video

Mission Control, the accessibility engine were rewritten in Swift.

  • 15% less code in Swift than in Objective C
  • Engineers love using Swift

Open source – swift.org

  • Big emphasis of Swift 3
  • Everyone follows the same procedure for getting changes incorporated, even Apple, using the swift-evolution repository on GitHub

Naming

New features for controlling how Objective-C APIs are imported into Swift

Whole module optimization is now turned on for al new projects.  Existing projects will get a warning to modernize.

Swift API Guidelines

Video

Principles

  • Clarity at the point of use
    • Design APIs to make uses clear and concise
    • Uses of APIs always have surrounding context
    • Don’t optimize for bad code
  • Clarity is more important than brevity
    • Brevity itself is not a worthwhile goal
  • Concise code is a consequence of using contextual cues

Omit needless words, those that don’t contribute to clarity

Brevity itself is not a worthwhile goal

Make uses of your APIs read grammatically

  • If the first argument is part of a prepositional phrase, give it a label
  • If the first argument is not pat of a grammatical phrase, give it a label
  • Use a verb to describe the side effect
  • Use a noun to describe a result

Name methods based on their side effects

  • Use a verb to describe the side effect
  • Use a noun to describe the result

Mutating/non-mutation pairs

  • ed/ing rule
    • “reverse” – mutating
    • “reversed” – non-mutating

Objective-C names in Swift

  • Avoid strings for selectors and key paths.
  • Use #selector for selectors (introduced in Swift 2.2).  Swift 3 adds support for getters and setters.
  • Use #keyPath for key paths (introduced in Swift 3)
  • NS_EXTENSIBLE_STRING_ENUM – allows you to provide stronger type-safety for Objective-C string values that are intended to be used in a specific way.

Migrating code

  • Swift 3 migrator
  • Compiler helps with renaming-related problems
  • Near-miss detection

Using Store Kit for In-App Purchases with Swift 3

Video

  • Expanded categories
  • Increased proceeds for longer held subscribers (longer than a year)
  • Territory pricing
  • Preserve prices
  • Upgrades and downgrades

Types of in-app purchases

  1. Consumable products
  2. Non-consumable products
  3. Non-renewing subscriptions
  4. Auto-renewing subscriptions

Rely on the payment queue and only the queue

What's New in Cocoa Touch

Video

Core technologies

  • Name changes in Swift
  • Grand Central Dispatch
  • Foundation
    • New ISO 8601 date formatter

UIPasteboard

  • A paste operation might have to retrieve remote content
  • Control over content

UIGraphicsRenderer

  • Fully color managed
  • Block-based
  • Subclasses for images and PDF
  • Manages context lifetime

Asset Management

  • Compression

Accessibility Inspector

  • View and audit accessibility support

Speech Recognition

  • SFSpeechRecognizer
  • Continuous speech recognition
  • From audio files or buffers

Text content type

Dynamic Type

  • Content size
    • No longer a property on UIApplication

Tab Bar customization

Improved WKWebView support for peek and pop

Custom peek and pop animation on UIPreviewInteraction

Full support for UIRefreshControl on UIScrollView and UICollectionView

Collection view

  • Now supports automatic self-sizing cells in flow mode
  • Smooth scrolling with cell prefetching, cell at a time, not by row, for free
  • Data prefetching – new protocol, also on table views

Animation

  • Interruptible
  • Scrubbable
  • Reversible
  • Rich timing features
  • Dynamic
  • Interactive and gesture-driven, all in one animation

Improving openURL

  • Async with completion handler
  • Let’s you check if a handler app is installed

NSUserActivity

  • Capture the state of your application
  • Infrastructure for Handoff, Spotlight
  • Now supports locations
  • Search results can now be continued in your app

SceneKit

  • New realistic rendering
  • Available on watchOS

New display modes for widgets

  • Widgets can be shown on the lock screen
  • Consider privacy in your use of this feature

New user notification framework

  • Unifies local and remote notifications
  • Better delivery management
  • In-app presentation option
  • Multi-platform (iOS, watchOS, tvOS)
  • Service extensions
    • Non-UI extension point
  • Content extension
    • Custom views
    • Cannot interact

CallKit

  • Directory extension
  • Blocking
  • Identification
  • Call provider API

SiriKit

  • Intents and responses
  • Intents are domain specific
  • IntentsUI extension for displaying your own UI

What's New in watchOS 3

Video

Watch app objectives

  • Glanceable
  • Actionable
  • Responsiveness

The Glance functionality has been merged into the new Dock

Spend time figuring out how to shorten interactions with the watch

Background App Refresh

All the user’s favorite apps are in memory.  They are launched and then suspended.  This includes apps associated with complications and those placed in the Dock.

The OS takes a snapshot of an app to show in the dock the last time it was visible.  Use the background refresh APIs to update the snapshot.

The system schedules a refresh when:

  • The user leaves your app
  • Your notification is dismissed

You may want to snapshot a simpler view for display in the dock and then transition to a more complete view when the user brings the app to the foreground

Silent Complication Push Update

  • Each app on the watch face can get up to 50 push updates

If the dock is full, each app in the dock can be updated once per hour

If it is not full, the total number of updates will be distributed amongst the remaining apps

Your app on the phone can also push updates to the app on the watch

Navigation Enhancements

Use segues so that the watch system can generate views in the hierarchy

Fit as much of your detail view on a single “page”

Crown

You can use the crown to scrub through a graph

This requires that you don’t use a scroll view

Introduction to Notifications

Video

New User Notifications Framework

  • Feature parity with existing API
  • Same code path for local and remote notification handling
  • Your app has access to pending and delivered notifications
  • In-app presentation optino

Local notifications on the watch

Access to user-defined settings

Title always shown.  Subtitle available as well.  Media attachments can also be shown.

Triggers

  • Push (remote only)
  • Time interval
  • Calendar
  • Location

Notification Management

  • Access to pending and delivered notifications
  • Remove notifications
  • Update notifications
  • New field on the HTTP/2 request header on push notifications

Notification Actions

  • Default action
  • Custom actions
    • Background (lets your app process the results in the background
    • Foreground (launches your app)
    • iOS and watchOS
  • Dismiss action (new in iOS 10)
    • Action performed when user dismisses or clears the notification

Service Extension

  • Non-UI iOS extension (runs in the background)
  • Purpose: Augment or replace the content of visible remote notifications
  • Short execution time
  • Fallback called
  • Can perform end-to-end encryption and add attachments
  • New key – “mutable-content” – to signal that the extension should be called

Q: How can service extensions be supported for multiple versions of iOS?

Advanced Notifications

Video

Media Attachments

  • Requires using the new Service Extension feature in iOS 10
  • Allows the app to download the media before the notification is displayed to the user
  • Available for local and remote notifications
  • Limited processing time and size
  • When added, file is moved and managed by the system
  • GIFs are supported

Custom User Interface

  • Allows you to add your own views
  • No interaction with your views
  • Your app can respond to notification actions

Quick Interaction Techniques for watchOS

Video

What is a quick interaction?

  • Starting a timer
  • Checking the weather
  • Checking an item on your grocery list

Apps need to be:

  • Glanceable
  • Actionable
  • Responsive

How long is a quick interaction?  2 seconds

New kinds of user input

  • Gesture recognizers
  • Digital crown rotation
  • Improved table navigation
  • Support for new notifications framework
  • Support for SpriteKit and SceneKit

New gestures

  • Swipe (discrete)
  • Tap (discrete)
  • Pan (continuous)
  • Long Press (continuous)

Add gestures to groups instead of controls

  • Some system gestures will have precedence
    • Scroll, Force Touch, Long Press

Digital Crown Support

  • Rotations per second
  • Rotational delta (1 full rotation = accumulated delta of 1)

Vertical paging

  • Kind of like paging on iOS – user doesn’t have to go back to table
  • Must use segues for the table

Notifications

  • Similar notification APIs across platforms
  • Can schedule local notifications
  • Background actions now delivered to extension if scheduled on the watch
  • If scheduled on the watch, only delivered on the watch
  • Notification identifiers used to avoid duplicate alerts
    • Apple Watch notifications take precedence over forwarded notifications with the same ID

Complications

  • Recommendation is that all apps provide a complication
  • Providing a complication keeps app in memory
  • Accelerates users into your app
  • Complications can be configured on the phone

SpriteKit & SceneKit

  • Not just for gaming
  • SpriteKit is a good choice for graphing
  • Can use the digital crown for scrubbing across a graph

Drop Wrist

  • Avoid showing the loading indicator
  • Show feedback and then use a background NSURLSession and local notifications

Designing Great Apple Watch Experiences

Video

3 components of a watch app

  • Complication – Any feature on a watch face beyond showing hours, minutes, seconds
  • Notification
  • App

Challenge yourself to use quicker and less complicated user flows

Shoot for 2 second user flows

If you’re going to make an Apple Watch app, you should make a complication

Adding extra large complication style

  • The watchOS Human Interface Guidelines describe all the styles and sizes in detail

Glanceable Notifications

  • PRoide a meaningful title
  • Only show what’s necessary
  • Present important information first
  • Establish visual hierarchy
  • Use color, graphics, and animation

Snapshot Best Practices

  • Anticipate user interests
  • Account for user preferences and contextual factors
  • Be logical and predictable
  • Be recognizable – not radically different than your app
  • Awkward views
  • Prepare to get small

Glances evolved into the Dock, which then caused Glances to become redundant

Simplified Navigation

Stickiness

  • Apps can stick around for 8 minutes on watchOS 3

Improving Existing Apps with Modern Best Practices

Video

Minimum deployment of iOS 8 – this hits 95% of all iOS devices

  • Set it to iOS 8.4.  When iOS 10 is released, set the minimum to iOS 9.3

Resolve issues, such as deprecated APIs

Treat warnings as errors.  Now available for Swift in Xcode 8

Accessibility is as much a part of your user interface as the artwork

New locale-aware APIs

  • Dimension and MeasurementFormatter

Add peek, pop, and quick actions

Run the Swift Migrator

File bug reports – bugreport.apple.com

Incorporate new APIs

Asset Catalogs

Use asset catalogs

UIImage(named:) finds image files whether they are stored in an asset catalog or not

  • It also performs caching

Scaled Assets

  • Provide all versions to avoid scaling on the device
    • Scaling up produces blocky image
    • Scaling down uses a lot of memory on the device

Vector Assets

  • Scaled and rasterized at build time

Both

  • Allows you to provide overrides, e.g. if there is too much detail on the vector to be displayed properly in 1x

Rounded/Capped Images

  • Slices can now be performed in the asset editor in Xcode

Dependency Injection

Don’t put everything in the app delegate and access it from everywhere.

  • Instead, inject required components

Other

Start with data bundled with your app and then download an update.  That way the app starts with data.

Playgrounds

You can now run code in playgrounds that finished asynchronously

  • Import PlaygroundSupport

Can show live views in the playground (e.g. a table view)

You can now include code and resources (e.g. plist files) in a playground that is not in the main window of the playground

Adding Platforms

Design for the platform

Pivot the data model

Visual Debugging with Xcode

Video

Runtime issues

  • Elevates issues encountered at run time to the same level as build time issues
    • Threading issues
    • UI layout issues
    • Memory leaks

Thread sanitizer

  • Data races
  • Use of uninitialized mutexes
  • Unlock from wrong thread
  • Thread leaks
  • Unsafe calls in signal handlers

Xcode can now render blurred views starting with Xcode 8 beta 2

More Auto Layout properties are displayed

Ambiguous layouts are detected at runtime

Advances in UIKit Animations and Transitions

Video

UIViewPropertyAnimator

  • Interruptible
  • Scrubbable
  • Reversible
  • Broad availability of timing functions
  • Running animations can be modified

What's New in UICollectionView in iOS 10

Video

Topics

  • Smooth scrolling
  • Improvements to self-sizing cells
  • Interactive reordering

Smooth Scrolling

  • Cells will be brought in before they are needed
  • Cells will be brought in one at a time instead of a row at a time

Cell Pre-Fetching

  • Enabled by default for apps compiled on iOS 10
  • Can be disabled
  • Do heavy lifting in cellForItemAtIndexPath
  • Do minimal work in willDisplayCell / didEndDisplayingCell
  • cellForItemAtIndexPath prepared cell may never display
  • New delegate for pre-fetching: UICollectionViewDataSourcePrefetching with prefetch and cancel prefetching functions
    • This allows fetching to be performed on a background thread

Pre-Fetching API Tips

  •  Use GCD or NSOperationQueue
  • Use cancelPrefetching API to adapt to shifting user focus

Same functionality also available to UITableView

  • UITableViewDataSourcePrefetching

Improvements to Self-Sizing Cells

Introduced in iOS 8

  • Full support in UICollectionViewFlowLayout – set estimatedItemSize
  • Three options for specifying actual cell size: auto layout, override sizeThatFits(), override preferredLayoutAttributesFittingAttributes()

Picking a good estimated item size

  • Hard to guess
  • Now can be automated in iOS 10 with UICollectionViewFlowLayoutAutomaticSize
  • iOS will use the sizes of all the other cells to estimate the size
  • Works best if cells have similar widths and heights

Interactive Reordering

  • Added in iOS 9
  • Paging support added in iOS 10
  • Behaves similar to reordering apps on the home screen

UIRefreshControl

Now supported directly in UICollectionView, UITableView, and UIScrollView because it is a scroll view property

NSURLSession: New Features and Best Practices

Video

NSURLConnection officially deprecated in 2015

Good sessions from WWDC 2014 and 2015 on NSURLSession

Benefits

  • HTTP/1.1, SPDY, HTTP/2 (RFC 7540)
  • App Transport Security (ATS)
  • HTTP Strict Transport Security (HSTS) – no code changes required
  • Cache, cookies, proxy, authentication
  • Configuration

Avoid 1-task-to-1-session model

  • Have 1 session service many tasks
    • Better performance and resource utilization

HTTP/2 Protocol

  • Multiplexing and concurrency
  • Header compression
  • Stream priorities
  • Server Push (new)

HTTP/2 Server Push

  • Prevent network round trips
  • Server support required
  • Now available in NSURLSession
  • Provides 2x to 3x improvement

Network Statistics

  • NSURLSessionTaskMetrics
  • NSURLSessionTaskTransactionMetrics
    • Request and response
    • Protocol and connection
    • Load info
    • Connection establishment and transmission

Transport Layer Security (SSL) is the predecessor

  • Protects data
  • The RC4 cipher is no longer supported

Command-line tool – nscurl

  • nsurl — enable-rc4 – use to find dependencies on RC4

App Transport Security

  • 2 new keys
    • NSAllowsArbitraryLoadsInWebContent
    • NSRequiresCertificateTransparency

Making Apps Adaptive, Part 1

Video

Takeaway

  • The system is going to do most of the work so you don’t have to

Traits – describe the environment that your application is running in

  • Layout
    • Size Classes
    • Dynamic Type
    • Layout Direction
  • Appearance
    • Display Gamut
    • Interface Style
  • Capabilities
    • 3D Touch

Size Classes

  • Screen size
  • Orientation
  • Adaptation (e.g. multi-tasking)

Takeaway

  • Base layout on available space, not device, orientation, or adaptation
  • Size classes express (user) experience

Only have to think about four combinations of size classes

  • But most commonly just two, because width is most common

If you use UISplitViewController, changes between master-detail and navigation will come for free

traitCollectionDidChange: is the key method for adapting changes in traints

Interface Builder is using the simulator to render views

  • You can now quickly switch between devices and orientations to view how a storyboard would be rendered

Architecting for Performance on watchOS 3

Video

2-second tasks

Complications for all

  • All Apple apps now have a complication

Memory limit

  • 30 MB per application in watchOS 3

Tips for keeping memory footprint down

  • Use appropriately sized images
  • Use appropriately sized data sets
  • Use lightweight APIs
  • Don’t keep around things you no longer need

Resume Time

  • Key path to optimize

Lifecycle – extension delegate

  • applicationDidFinishLaunching
  • applicationDidBecomeActive
  • applicationWillResignActive
  • applicationDidEnterBackground
  • applicationWillEnterForeground (not on first launch)

Lifecycle – interface controller

  • awakeWithContent
  • willActivate
  • didAppear
  • willDisappear
  • didDeactivate

Resume Time Tips

  • Use discretion when updating WKInterface objects
    • Setting a property sends data to the watch, so only set them if they have changed
  • WKInterfaceTable is not a UITableView
    • All cells are created up front
    • Keep the size down – it’s best to keep the size to a maximum of just over 20
    • Avoid reloading a table when possible – use insertion and deletion APIs

Design

  • Glanceable
  • Focused purpose
  • Navigation

Improve layout update performance

  • Setting values on a WKInterfaceConnector are not cached, so only set them wifi they have changed

Think small

  • Keep tasks small and easy to perform
  • Simplify your user interface
  • Make use of new Background Refresh APIs

Focus on resume time

  • Pay attention to WKInterfaceController lifecycle methods (especially willActivate and didAppear)
  • Make use of cancelable operations
  • Optimize when updating your user interface

A Peek at 3D Touch

Video

Static Quick Actions

  • Defined in your app’s Info.plist
  • Available as soon as your app has been installed

Dynamic Quick Actions

  • Created by your app at runtime
  • Available after the first launch of your app
  • Shows after any static quick actions (space permitting) – only 4 total
  • Can include a system icon, custom icon, or Address Book contact

Best Practices

  • Every app should provide quick actions
  • Focus on providing quick access to high-value tasks
  • Make quick actions predictable
  • Be prepared to handle dynamic quick actions from previous versions of your app
    • Include the app version in the info provided with the dynamic action
  • Don’t add functionality that is only accessible using quick actions

Register for previewing in viewDidLoad

  • Set self as the delegate

Make sure that similar looking stuff is previewable in similar ways

Preview quick actions

Peek and Pop Best Practices

  • Content that can be tapped should support Peek and Pop
  • Return a preview view controller consistently – similar looking content should be previewable in a similar way
  • Don’t take too long in the previewing delegate
  • Set the previewing context sourceRect to lift the previewing interface element on-screen

UIPreviewInteraction

Low-Level Force API

  • Normalized access to force data

Using and Extending the Xcode Source Editor

Video

New in Xcode 8

  • New font – SFMono – designed for retina displays
  • Current line highlight
  • Command-Option-Slash – inserts documentation comment block
  • Documentation comments use SFMono-medium for keywords and SFMono-light for other text
  • Image and color literals

Not new

  • Command-Option-] – Selects lines and allows you to move them
  • Command-Control-G – Navigates to search results

Source Editor Extensions

  • Add commands to the source editor
  • Edit text
  • Change selections
  • One extension, several commands
  • They are Application Extensions
    • Each runs in its own process
    • Sandboxed and uses entitlements
    • Gets access to text at invocation
    • Distributable on the Mac App Store

Built into a host application

Extension commands:

  • Your extension’s Info.plist
  • Your extension’s commandDefinitions property, overriding the Info.plist
    • Overrides collection stored in the Info.plist

Speed

  • Text editing is “user-synchronous”
  • Users will invoke your command via typing
  • User changes to a document are prevented while a command is running
  • The user can cancel your command
    • A command that takes a while gets a cancellation banner

How Xcode helps

  • Keeps your extension alive for fast invocation
  • Optimizes data transfer for performance

How you can help Xcode

  • Start up quickly
  • Use GCD and follow standard asynchronous patterns
  • Don’t replace the whole buffer if you don’t need to

Making Apps Adaptive, Part 2

Video

Asset Catalogs

Alignment Insets and Slicing

Dynamic Type

  • Now part of the trait environment with UITraitCollction.preferredContentSizeCategory
  • Set up your text views
    • .font = UIFont.preferredFont(forTextStyle:)
    • .adjustsFontForContentSizeCategory = true
  • Be sure to test with all Dynamic Types sizes

Layout Guides

  • UIView.layoutMarginGuid
  • UIView.readablContentGuide

UIAppearance

Design you app for different adaptations

  • Consider the possible combinations of device, orientation, and size
  • Make designs
  • Define rules for which design to use

Defining Rules

  • Don’t check if size exactly matches
  • Do define Boolean conditions
    • Use size class
    • Compare value to a threshold
    • Compare values to each other

Implement the Designs

  • Find the size of the app
  • Use the rules to decide which design to use
  • Apply the design to the UI

Where does the code go?

  • Don’t put design-specific code in viewDidLoad
  • Put it in viewWillLayoutSubviews
  • This is called very often, so do as little work as possible
  • Find out what has changed since last time
  • Don’t cause a layout loop

Reusable Elements

  • Build your app out of pieces that can be reused in different designs
  • Each piece is typically a view controller
  • View tree and constraints
  • Connections to other view controllers
  • Connections to the rest of your app

Design Object

  • Stores information describing a design
  • Immutable struct for safety
  • Allows comparison

Iterative UI Design

Understanding Swift Performance

Video

Understanding the implementation to understand the performance

Dimensions of Performance

  • Allocated on the stack or the heap
  • How much reference counting
  • Statically or dynamically dispatched

Allocation

Stack – very fast

  • Decrement stack point to allocation
  • Increment stack pointer to deallocate

Heap

  • Advanced data structure
    Search for unused block of memory to allocate
  • Reinsert block of memory to deallocate
  • Thread safety overhead

Structs are allocated on the stack

Class references are allocated on the stack, instances on the heap.  It also allocates more memory than is needed

Changing an instance of a class will also change other instances.

Reference Counting

There’s more to reference counting than incrementing, decrementing

  • Indirection
  • Thread safety overhead

There’s no reference count overhead with a struct that has only value type members

A struct incurs reference counting overhead proportional to the number of reference type members

Foundation added UUID type in iOS 10 that’s a value type (struct)

An enum backed by a string doesn’t require heap allocation

Method Dispatch

Static

  • Jump directly to implementation at run time
  • Candidate for inlining and other optimizations

Dynamic

  • Look up implementation in table at run time
  • Then jump to implementation
  • Prevents inlining and other optimizations

Marking a class as final will allow the compiler to statically dispatch functions on that class

Performance Types

  • Polymorphism without inheritance or reference semantics
  • Protocol Witness Table (PWT)
  • Dynamic dispatch without a V-Table
  • The Existential Container
  • Boxing values of protocol types
    • Inline Value Buffer – currently 3 words
  • Large values stored on heap
    • The Value Witness Table (VWT)
    • Allocation, Copy, Destruction of any Value
  • Reference to Value Witness Table
  • Reference to Protocol Witness Table
// Protocol Types
// The Existential Container in action
func drawACopy(local: Drawable) {
    local.draw()
}
let val: Drawable = Line()
drawACopy(val)
// Generated code
func drawACopy(val: ExistContDrawable) {
    var local = ExistContDrawable()
    let vwt = val.vwt
    let pwt = val.pwt
    local.type = type
    local.pwt = pwt
    vwt.allocateBufferAndCopyValue(&local, val)
    pwt.draw(vwt.projectBuffer(&local))
    vwt.destructAndDeallocateBuffer(temp)
}

Protocol Type Stored Properties

struct Pair {
    init(_ f: Drawable, _ s: Drawable) {
        first = f
        second = s
    }
    var first: Drawable
    var second: Drawable
}

var pair = Pair(Line(), Point())

Expensive Copies of Large Values

let aLine = Line(1.0, 1.0, 1.0, 3.0)
let pair = Pair(aLine, aLine)
let copy = pair

References Fit in the Value Buffer

Indirect Storage with Copy-on-Write

Use a reference type for storage

class LineStorage { var x1, y1, x2, y2: Double }
struct Line: Drawable {
    var storage: LineStorage
    init() { storage = LineStorage(Point(), Point()) }
    func draw() { ... }
    mutating func move() {
        if !isUniquelyReference(&storage) {
            storage = LineStorage(storage)
        }
        storage.start = ...
    }
}

Summary – Protocol Types

  • Dynamic polymorphism
  • Indirection through Witness Tables and Existential Container
  • Copying of large values causes heap allocation

Generic Code

protocol Drawable {
    func draw()
}
func drawACopy(local: T) {
    local.draw()
}

let line = Line()
drawACopy(line)
// ...
let point = Point()
drawACopy(point)

Generic code implements static polymorphism – one type per call context

  • Type substituted down the call chain

Implementation of Generic Methods

  • One shared implementation
  • Uses Protocol/Value Witness Table
  • One type per call context – passes tables instead of using existential container

Specialization of Generics

  • Static polymorphism – uses type at call-site
  • Creates type-specific version of method
  • Version per type in use
  • Can be more compact after optimization

When does specialization happen?

  • Infer type at call-site
  • Definition must be available

Whole Module Optimization

  • Increases optimization opportunity
  • Now enabled by default in Xcode 8

Specialized Generics – Struct Type

  • Performance characteristics like struct types
    • No heap allocation on copying
    • No reference counting
    • Static method dispatch

Specialized Generics – Class Type

  • Performance characteristics like class types
    • Heap allocation on creating instance
    • Reference counting
    • Dynamic method dispatch through V-Table

Unspecialized Generics – Small Value

  • No heap allocation – value fits in Value Buffer
  • No reference counting
  • Dynamic dispatch through Protocol Witness Table

Unspecialized Generics – Large Value

  • Heap allocation (use indirect storage as a workaround)
  • Reference counting if value contains references
  • Dynamic dispatch through Protocol Witness Table

Summary

  • Choose fitting abstraction with the least dynamic runtime type requirements
    • Enables static type checking, allows compiler to ensure correctness at compile time
    • struct types – value semantics
    • class types – identity or OOP style polymorphism
    • Generics – static polymorphism
    • Protocol types – dynamic polymorphism
  • Use indirect storage to deal with large values