Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer...

218
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Protocol and Value Oriented Programming in UIKit Apps Swift in practice Developer Tools #WWDC16 Session 419 Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer

Transcript of Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer...

Page 1: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

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

Protocol and Value Oriented Programming in UIKit AppsSwift in practice

Developer Tools #WWDC16

Session 419

Jacob Xiao Protocol Oriented ProgrammerAlex Migicovsky Swift Compiler Typo Engineer

Page 2: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Local Reasoning

Page 3: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Model View Controller

Controller

Model View

Page 4: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Lucid Dreams

Page 5: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 6: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 7: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 8: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 9: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 10: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 11: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 12: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 13: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Think Different

Protocol-Oriented Programming in Swift WWDC 2015

Building Better Apps with Value Types in Swift WWDC 2015

Page 14: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Overview

Value types and protocols• Recap—Model• Focus—View and controller • Testing

Sample code: https://developer.apple.com/go/?id=lucid-dreams

Page 15: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

What’s a dream?Model

Page 16: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Reference Semantics

class Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

Page 17: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Saw the light

.unicorn(.yellow)

dream2

// Reference Semantics

class Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream1

Page 18: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Saw the light

.unicorn(.yellow)

dream2

// Reference Semantics

class Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream1

Page 19: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Saw the light

.unicorn(.yellow)

dream2

Unicorns all over

// Reference Semantics

class Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream1

Page 20: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Saw the light

.unicorn(.yellow)

dream2

Unicorns all over

// Reference Semantics

class Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream1

?!@##?

Page 21: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Relationships

App Delegate

Navigation VC

Dreams VC Favorite Creature VCDream VC

Dream Creature

Page 22: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Relationships

App Delegate

Navigation VC

Dreams VC Favorite Creature VCDream VC

Dream Creature

Page 23: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Relationships—It's Complicated…

App Delegate

Navigation VC

Dreams VC Favorite Creature VCDream VC

CreatureDream

Page 24: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Relationships—It's Complicated…

App Delegate

Navigation VC

Dreams VC Favorite Creature VCDream VC

CreatureDream

Page 25: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

App Delegate

Navigation VC

Dreams VC Favorite Creature VCDream VC

Dream Creature

Relationships—It's Complicated…

Page 26: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Value Semantics

struct Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1 dream2

Saw the light

.unicorn(.yellow)

dream1

Saw the light

.unicorn(.yellow)

Page 27: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Value Semantics

struct Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream2

Saw the light

.unicorn(.yellow)

dream1

Saw the light

.unicorn(.yellow)

Unicorns all over

Page 28: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Value Semantics

struct Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream2

Saw the light

.unicorn(.yellow)

dream1

Saw the light

.unicorn(.yellow)

Unicorns all over

Page 29: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Value Semantics

struct Dream {

var description: String

var creature: Creature

var effects: Set<Effect>

...

}

var dream1 = Dream(...)

var dream2 = dream1

dream2.description = "Unicorns all over"

dream2

Saw the light

.unicorn(.yellow)

dream1

Saw the light

.unicorn(.yellow)

Unicorns all over

😃

Page 30: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

“Use values only for simple model types.”

The Internet™

Page 31: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

“Use values only for simple model types.”

The Internet™

Page 32: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell layoutView

Jacob Xiao Protocol Oriented Programmer

Page 33: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 34: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

DecoratingLayoutCell

UIView

UIKit

Page 35: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

DecoratingLayoutCell

UIView

UIKit

Page 36: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

DecoratingLayoutCell

UIView

UIKit

Page 37: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

DecoratingLayoutCell

DreamCell

UIView

Saw the light

UIKitUITableViewCell

Page 38: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

DecoratingLayoutCell

DreamCell

DreamDetailView

UIView

Saw the light

Saw the light

UIKitUITableViewCell

Page 39: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

Layout

Page 40: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

LayoutUIView

Page 41: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Cell Layout

UITableViewCell

LayoutUIView SKNode

Page 42: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Cell Layout

class DecoratingLayoutCell : UITableViewCell {

var content: UIView

var decoration: UIView

// Perform layout...

}

Page 43: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

struct DecoratingLayout {

var content: UIView

var decoration: UIView

// Perform layout...

}

Page 44: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

struct DecoratingLayout {

var content: UIView

var decoration: UIView

mutating func layout(in rect: CGRect) {

// Perform layout...

}

}

Page 45: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

class DreamCell : UITableViewCell {

...

override func layoutSubviews() {

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: bounds)

}

}

Saw the light

Page 46: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

class DreamCell : UITableViewCell {

...

override func layoutSubviews() {

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: bounds)

}

}

class DreamDetailView : UIView {

...

override func layoutSubviews() {

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: bounds)

}

}

Saw the light

Page 47: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

class DreamCell : UITableViewCell {

...

override func layoutSubviews() {

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: bounds)

}

}

class DreamDetailView : UIView {

...

override func layoutSubviews() {

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: bounds)

}

}

Saw the light

Page 48: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

Page 49: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = UIView()

let child2 = UIView()

Page 50: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = UIView()

let child2 = UIView()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

Page 51: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = UIView()

let child2 = UIView()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

Page 52: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = UIView()

let child2 = UIView()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

XCTAssertEqual(child1.frame, CGRect(x: 0, y: 5, width: 35, height: 30))

XCTAssertEqual(child2.frame, CGRect(x: 35, y: 5, width: 70, height: 30))

}

Page 53: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Local ReasoningEasier to understand, easier to test

Page 54: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

struct DecoratingLayout {

var content: UIView

var decoration: UIView

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 55: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// View Layout

struct DecoratingLayout {

var content: UIView

var decoration: UIView

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 56: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// SpriteKit Layout

struct ViewDecoratingLayout {

var content: UIView

var decoration: UIView

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

struct NodeDecoratingLayout {

var content: SKNode

var decoration: SKNode

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 57: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

struct NodeDecoratingLayout {

var content: SKNode

var decoration: SKNode

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

// Layout

Page 58: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content:

var decoration:

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 59: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content:

var decoration:

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 60: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

Page 61: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

Page 62: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Page 63: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Page 64: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Page 65: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

UIView

Page 66: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout {

var content: Layout

var decoration: Layout

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

SKNode

UIView

Page 67: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout<Child : Layout> {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Page 68: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout<Child : Layout> {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Page 69: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

struct DecoratingLayout<Child : Layout> {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

protocol Layout {

var frame: CGRect { get set }

}

extension UIView : Layout {}

extension SKNode : Layout {}

Must be the same

Page 70: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Generic Types

More control over typesCan be optimized more at compile time

Page 71: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Generic Types

More control over typesCan be optimized more at compile time

Understanding Swift Performance Mission Friday 11:00AM

Page 72: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Sharing Code

Page 73: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Sharing Code

Page 74: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Sharing Code

Page 75: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

var dstRect = contentView.bounds

dstRect.size.width /= 3

return dstRect

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

}

class DreamCell: DecoratingLayoutCell {

// MARK: Properties

static let reuseIdentifier = "\(DreamCell.self)"

var content = UILabel()

var accessories = [UIImageView]()

var dream: Dream! {

didSet {

// Update the UI when the `dream` changes.

accessories = (0..<dream.numberOfCreatures).map { _ in

let imageView = UIImageView(image: dream.creature.image)

imageView.contentMode = .scaleAspectFit

return imageView

}

content.text = dream.description

for subview in contentView.subviews {

subview.removeFromSuperview()

}

addSubviews()

setNeedsLayout()

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubviews()

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

private func addSubviews() {

let multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

for view in multiPaneLayout.contents {

contentView.addSubview(view)

}

}

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

multiPaneLayout.layout(in: contentView.bounds)

}

}

// Inheritance

Page 76: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

class DecoratingLayoutCell : UITableViewCell {

var content: UIView!

var decoration: UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

decoration.contentMode = .scaleAspectFit

contentView.addSubview(content)

contentView.addSubview(decoration)

setNeedsLayout()

}

override func layoutSubviews() {

super.layoutSubviews()

content.frame = contentLayoutFrame

decoration.frame = decorationLayoutFrame

}

var contentLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.origin.x = rect.size.width / 3

dstRect.size.width *= 2/3

return dstRect

}

var decorationLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.size.width /= 3

return dstRect

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

}

class DreamCell: DecoratingLayoutCell {

// MARK: Properties

static let reuseIdentifier = "\(DreamCell.self)"

var content = UILabel()

var accessories = [UIImageView]()

var dream: Dream! {

didSet {

// Update the UI when the `dream` changes.

accessories = (0..<dream.numberOfCreatures).map { _ in

let imageView = UIImageView(image: dream.creature.image)

imageView.contentMode = .scaleAspectFit

return imageView

}

content.text = dream.description

for subview in contentView.subviews {

subview.removeFromSuperview()

}

addSubviews()

setNeedsLayout()

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubviews()

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

private func addSubviews() {

let multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

for view in multiPaneLayout.contents {

contentView.addSubview(view)

}

}

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

multiPaneLayout.layout(in: contentView.bounds)

}

}

class CreatureCell: DreamCell {

// MARK: Properties

static let reuseIdentifier = "\(CreatureCell.self)"

// Inheritance

Page 77: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

class DecoratingLayoutCell : UITableViewCell {

var content: UIView!

var decoration: UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

decoration.contentMode = .scaleAspectFit

contentView.addSubview(content)

contentView.addSubview(decoration)

setNeedsLayout()

}

override func layoutSubviews() {

super.layoutSubviews()

content.frame = contentLayoutFrame

decoration.frame = decorationLayoutFrame

}

var contentLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.origin.x = rect.size.width / 3

dstRect.size.width *= 2/3

return dstRect

}

var decorationLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.size.width /= 3

return dstRect

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

}

class DreamCell: DecoratingLayoutCell {

// MARK: Properties

static let reuseIdentifier = "\(DreamCell.self)"

var content = UILabel()

var accessories = [UIImageView]()

var dream: Dream! {

didSet {

// Update the UI when the `dream` changes.

accessories = (0..<dream.numberOfCreatures).map { _ in

let imageView = UIImageView(image: dream.creature.image)

imageView.contentMode = .scaleAspectFit

return imageView

}

content.text = dream.description

for subview in contentView.subviews {

subview.removeFromSuperview()

}

addSubviews()

setNeedsLayout()

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubviews()

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

private func addSubviews() {

let multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

for view in multiPaneLayout.contents {

contentView.addSubview(view)

}

}

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

multiPaneLayout.layout(in: contentView.bounds)

}

}

class CreatureCell: DreamCell {

// MARK: Properties

static let reuseIdentifier = "\(CreatureCell.self)"

private var content = UILabel()

private var decoration = UIImageView()

var creature: Dream.Creature! {

didSet {

decoration.image = creature.image

}

}

var title = "" {

didSet {

content.text = title

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

decoration.contentMode = .scaleAspectFit

// Add our subviews based on the ordering of the decorating layout's contents.

let decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

for view in decoratingLayout.contents {

contentView.addSubview(view)

}

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: contentView.bounds)

}

}

// Inheritance

Page 78: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

class DecoratingLayoutCell : UITableViewCell {

var content: UIView!

var decoration: UIView!

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

decoration.contentMode = .scaleAspectFit

contentView.addSubview(content)

contentView.addSubview(decoration)

setNeedsLayout()

}

override func layoutSubviews() {

super.layoutSubviews()

content.frame = contentLayoutFrame

decoration.frame = decorationLayoutFrame

}

var contentLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.origin.x = rect.size.width / 3

dstRect.size.width *= 2/3

return dstRect

}

var decorationLayoutFrame: CGRect {

var dstRect = contentView.bounds

dstRect.size.width /= 3

return dstRect

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

}

class DreamCell: DecoratingLayoutCell {

// MARK: Properties

static let reuseIdentifier = "\(DreamCell.self)"

var content = UILabel()

var accessories = [UIImageView]()

var dream: Dream! {

didSet {

// Update the UI when the `dream` changes.

accessories = (0..<dream.numberOfCreatures).map { _ in

let imageView = UIImageView(image: dream.creature.image)

imageView.contentMode = .scaleAspectFit

return imageView

}

content.text = dream.description

for subview in contentView.subviews {

subview.removeFromSuperview()

}

addSubviews()

setNeedsLayout()

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

addSubviews()

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

private func addSubviews() {

let multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

for view in multiPaneLayout.contents {

contentView.addSubview(view)

}

}

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var multiPaneLayout = MultiPaneLayout(content: content, accessories: accessories)

multiPaneLayout.layout(in: contentView.bounds)

}

}

class CreatureCell: DreamCell {

// MARK: Properties

static let reuseIdentifier = "\(CreatureCell.self)"

private var content = UILabel()

private var decoration = UIImageView()

var creature: Dream.Creature! {

didSet {

decoration.image = creature.image

}

}

var title = "" {

didSet {

content.text = title

}

}

// MARK: Initialization

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {

super.init(style: style, reuseIdentifier: reuseIdentifier)

decoration.contentMode = .scaleAspectFit

// Add our subviews based on the ordering of the decorating layout's contents.

let decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

for view in decoratingLayout.contents {

contentView.addSubview(view)

}

}

required init?(coder aDecoder: NSCoder) {

fatalError("\(#function) has not been implemented")

}

// MARK: Layout

override func layoutSubviews() {

super.layoutSubviews()

/*

This is the intersection between the UIKit view code and this sample's

value based layout system.

*/

var decoratingLayout = DecoratingLayout(content: content, decoration: decoration)

decoratingLayout.layout(in: contentView.bounds)

}

}

// Inheritance

Page 79: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

CompositionShare code without reducing local reasoning

Page 80: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Composition of Views

Page 81: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Composition of Views

Page 82: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Composition of Views

Page 83: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Classes instances are expensive!

Composition of Views

Page 84: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Classes instances are expensive!

Composition of Views

Page 85: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Classes instances are expensive!

Composition of Views Values!

Page 86: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Classes instances are expensive!Structs are cheap

Composition of Views Values!

Page 87: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Classes instances are expensive!Structs are cheapComposition is better with value semantics

Composition of Views Values!

Page 88: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

struct CascadingLayout<Child : Layout> {

var children: [Child]

mutating func layout(in rect: CGRect) {

...

}

}

Page 89: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

struct CascadingLayout<Child : Layout> {

var children: [Child]

mutating func layout(in rect: CGRect) {

...

}

}

struct DecoratingLayout<Child : Layout> {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 90: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

struct CascadingLayout<Child : Layout> {

var children: [Child]

mutating func layout(in rect: CGRect) {

...

}

}

struct DecoratingLayout<Child : Layout> {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect) {

content.frame = ...

decoration.frame = ...

}

}

Page 91: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

protocol Layout {

var frame: CGRect { get set }

}

Page 92: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

protocol Layout {

mutating func layout(in rect: CGRect)

}

Page 93: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

protocol Layout {

mutating func layout(in rect: CGRect)

}

extension UIView : Layout { ... }

extension SKNode : Layout { ... }

Page 94: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

protocol Layout {

mutating func layout(in rect: CGRect)

}

extension UIView : Layout { ... }

extension SKNode : Layout { ... }

struct DecoratingLayout<Child : Layout> : Layout { ... }

struct CascadingLayout<Child : Layout> : Layout { ... }

Page 95: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

protocol Layout {

mutating func layout(in rect: CGRect)

}

extension UIView : Layout { ... }

extension SKNode : Layout { ... }

struct DecoratingLayout<Child : Layout, ...> : Layout { ... }

struct CascadingLayout<Child : Layout> : Layout { ... }

Page 96: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

let decoration = CascadingLayout(children: accessories)

var composedLayout = DecoratingLayout(content: content, decoration: decoration)

composedLayout.layout(in: rect)

Page 97: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

let decoration = CascadingLayout(children: accessories)

var composedLayout = DecoratingLayout(content: content, decoration: decoration)

composedLayout.layout(in: rect)

Page 98: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

let decoration = CascadingLayout(children: accessories)

var composedLayout = DecoratingLayout(content: content, decoration: decoration)

composedLayout.layout(in: rect)

Page 99: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Composition of Values

let decoration = CascadingLayout(children: accessories)

var composedLayout = DecoratingLayout(content: content, decoration: decoration)

composedLayout.layout(in: rect)

Page 100: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Contents

Page 101: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Contents

protocol Layout {

mutating func layout(in rect: CGRect)

var contents: [Layout] { get }

}

Page 102: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Contents

protocol Layout {

mutating func layout(in rect: CGRect)

var contents: [Layout] { get }

}

UIView and SKNode

Page 103: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

protocol Layout {

mutating func layout(in rect: CGRect)

associatedtype Content

var contents: [Content] { get }

}

Page 104: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct ViewDecoratingLayout : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content = UIView

var contents: [Content] { get }

}

ViewDecoratingLayout

UIViewUIView

Page 105: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type// Associated Type

struct ViewDecoratingLayout : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content = UIView

var contents: [Content] { get }

}

struct NodeDecoratingLayout : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content = SKNode

var contents: [Content] { get }

}

ViewDecoratingLayout

UIViewUIView

NodeDecoratingLayout

SKNodeSKNode

Page 106: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct NodeDecoratingLayout : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content = SKNode

var contents: [Content] { get }

}

ViewDecoratingLayout

UIViewUIView

NodeDecoratingLayout

SKNodeSKNode

Page 107: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout> : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content =

var contents: [Content] { get }

}

DecoratingLayout<UIView>

UIViewUIView

DecoratingLayout<SKNode>

SKNodeSKNode

Page 108: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout> : Layout {

...

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

DecoratingLayout<UIView>

UIViewUIView

DecoratingLayout<SKNode>

SKNodeSKNode

Page 109: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout> : Layout {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

DecoratingLayout

UIViewUIView

Page 110: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout> : Layout {

var content: Child

var decoration: Child

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

DecoratingLayout

UIViewCascadingLayout

UIView

Page 111: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout, Decoration : Layout

where Child.Content == Decoration.Content> : Layout {

var content: Child

var decoration: Decoration

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

Page 112: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout, Decoration : Layout

where Child.Content == Decoration.Content> : Layout {

var content: Child

var decoration: Decoration

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

Page 113: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Associated Type

struct DecoratingLayout<Child : Layout, Decoration : Layout

where Child.Content == Decoration.Content> : Layout {

var content: Child

var decoration: Decoration

mutating func layout(in rect: CGRect)

typealias Content = Child.Content

var contents: [Content] { get }

}

Page 114: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Layout

protocol Layout {

mutating func layout(in rect: CGRect)

associatedtype Content

var contents: [Content] { get }

}

Page 115: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = UIView()

let child2 = UIView()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

XCTAssertEqual(layout.contents[0].frame, CGRect(x: 0, y: 5, width: 35, height: 30))

XCTAssertEqual(layout.contents[1].frame, CGRect(x: 35, y: 5, width: 70, height: 30))

}

Page 116: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = TestLayout()

let child2 = TestLayout()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

XCTAssertEqual(layout.contents[0].frame, CGRect(x: 0, y: 5, width: 35, height: 30))

XCTAssertEqual(layout.contents[1].frame, CGRect(x: 35, y: 5, width: 70, height: 30))

}

struct TestLayout : Layout {

var frame: CGRect

...

}

Page 117: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// Testing

func testLayout() {

let child1 = TestLayout()

let child2 = TestLayout()

var layout = DecoratingLayout(content: child1, decoration: child2)

layout.layout(in: CGRect(x: 0, y: 0, width: 120, height: 40))

XCTAssertEqual(layout.contents[0].frame, CGRect(x: 0, y: 5, width: 35, height: 30))

XCTAssertEqual(layout.contents[1].frame, CGRect(x: 35, y: 5, width: 70, height: 30))

}

struct TestLayout : Layout {

var frame: CGRect

...

}

Page 118: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 119: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques

Page 120: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques

Local reasoning with value types

Page 121: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques

Local reasoning with value typesGeneric types for fast, safe polymorphism

Page 122: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques

Local reasoning with value typesGeneric types for fast, safe polymorphismComposition of values

Page 123: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoController

Alex Migicovsky Swift Compiler Typo Engineer

Page 124: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoController

Alex Migicovsky Swift Compiler Typo Engineer

zzz

Page 125: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 126: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 127: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 128: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 129: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 130: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 131: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 132: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

?!@##?

Page 133: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Undo Bug

class DreamListViewController : UITableViewController {

var dreams: [Dream]

var favoriteCreature: Creature

...

}

Model properties

Page 134: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Undo Registration

dreams

Page 135: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Undo Registration

dreams favoriteCreature

Page 136: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Undo Registration

dreams favoriteCreature

Page 137: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Undo Registration

dreams favoriteCreature

Page 138: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Undo Registration

dreams favoriteCreature

Page 139: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams favoriteCreature

Undo Registration

Page 140: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Model

dreams favoriteCreature

Undo Registration

Page 141: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Model

Undo Registration

dreams favoriteCreature favoriteMoment

Page 142: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Model

Undo Registration

dreams favoriteCreature favoriteMoment

Page 143: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

}

struct Model : Equatable {

}

var dreams: [Dream]

var favoriteCreature: Creature

Page 144: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

}

struct Model : Equatable {

}

var dreams: [Dream]

var favoriteCreature: Creature

Page 145: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

var model: Model

...

}

struct Model : Equatable {

var dreams: [Dream]

var favoriteCreature: Creature

}

Page 146: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast() tableView.deleteRows(at: ...)

UndoManager Stack

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

Model

.unicorn(.yellow)

dreams[0]

dreams[1] dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 147: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast() tableView.deleteRows(at: ...)

UndoManager Stack

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

Model

.unicorn(.yellow)

dreams[0]

dreams[1] dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 148: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.yellow)

dreams[0]

dreams.removeLast() tableView.deleteRows(at: ...)

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 149: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.yellow)

dreams[0]

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 150: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.yellow)

dreams[0]

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 151: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.pink)

dreams[0]

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 152: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.pink)dreams.append(Dream(...))

tableView.insertRows(at: ...)

dreams[0]

Page 153: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

Page 154: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast() tableView.deleteRows(at: ...)

UndoManager Stack

favoriteCreature = .unicorn(.pink) tableView.reloadRows(...)

Model

.unicorn(.yellow)

dreams[0]

dreams[1] dreams.append(Dream(...)) tableView.insertRows(at: ...)

Page 155: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast()

tableView.deleteRows(at: ...)

dreams.append(Dream(...))

tableView.insertRows(at: ...)

favoriteCreature = .unicorn(.pink)

tableView.reloadRows(...)

Page 156: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast()

tableView.deleteRows(at: ...)

dreams.append(Dream(...))

tableView.insertRows(at: ...)

dreams.insert(Dream(...), at: 5)

tableView.insertRows(at: ...)

favoriteCreature = .unicorn(.yellow)

tableView.reloadRows(...)

favoriteCreature = .unicorn(.white)

tableView.reloadRows(...)

dreams.insert(Dream(...), at: 5)

tableView.insertRows(at: ...)

favoriteCreature = .unicorn(.pink)

tableView.reloadRows(...)

Page 157: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

dreams.removeLast()

tableView.deleteRows(at: ...)

dreams.append(Dream(...))

tableView.insertRows(at: ...)

dreams.insert(Dream(...), at: 5)

tableView.insertRows(at: ...)

favoriteCreature = .unicorn(.yellow)

tableView.reloadRows(...)

favoriteCreature = .unicorn(.white)

tableView.reloadRows(...)

dreams.insert(Dream(...), at: 5)

tableView.insertRows(at: ...)

favoriteCreature = .unicorn(.pink)

tableView.reloadRows(...)

Page 158: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.yellow)

dreams[0]

dreams[1]

.unicorn(.yellow)

dreams[0]

.unicorn(.pink)

Page 159: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UndoManager StackModel

.unicorn(.yellow)

dreams[0]

dreams[1]

.unicorn(.yellow)

dreams[0]

.unicorn(.pink)

Page 160: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

.unicorn(.yellow)

dreams[0]

dreams[1]

.unicorn(.yellow)

dreams[0]

.unicorn(.pink)

UndoManager StackModel

Page 161: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

func modelDidChange(old: Model, new: Model) {

}

}

Page 162: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

func modelDidChange(old: Model, new: Model) {

if old.favoriteCreature != new.favoriteCreature {

// Reload table view section for favorite creature.

tableView.reloadSections(...)

}

}

}

Page 163: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

func modelDidChange(old: Model, new: Model) {

if old.favoriteCreature != new.favoriteCreature {

// Reload table view section for favorite creature.

tableView.reloadSections(...)

}

...

}

}

Page 164: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating the Model

class DreamListViewController : UITableViewController {

...

func modelDidChange(old: Model, new: Model) {

if old.favoriteCreature != new.favoriteCreature {

// Reload table view section for favorite creature.

tableView.reloadSections(...)

}

...

undoManager?.registerUndo(withTarget: self, handler: { target in

target.model = old

})

}

}

Page 165: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Benefits

Single code path

Page 166: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Benefits

Single code path• Better local reasoning

Page 167: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Benefits

Single code path• Better local reasoning

Values compose well with other values

Page 168: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

UI stateController

Page 169: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 170: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller
Page 171: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 172: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 173: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 174: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 175: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 176: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 177: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 178: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 179: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 180: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 181: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 182: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 183: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 184: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 185: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 186: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 187: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 188: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 189: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 190: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

viewing

selecting

Page 191: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

viewing

selecting

Page 192: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

viewing

selecting

sharing

selecting

viewing

Page 193: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 194: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

sharing

selecting

viewing

Page 195: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

var isInViewingMode: Bool

Page 196: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Invalid UI State Bug

class DreamListViewController : UITableViewController {

...

}

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

Page 197: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Invalid UI State Bug

class DreamListViewController : UITableViewController {

...

}

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

Page 198: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Invalid UI State Bug

class DreamListViewController : UITableViewController {

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

...

}

UI State

Page 199: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

case viewing

case sharing(dreams: [Dream])

case selecting(selectedRows: IndexSet)

// DreamListViewController — Isolating UI State

class DreamListViewController : UITableViewController {

...

}

enum State {

}

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

Page 200: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

case viewing

case sharing(dreams: [Dream])

case selecting(selectedRows: IndexSet)

// DreamListViewController — Isolating UI State

class DreamListViewController : UITableViewController {

...

}

enum State {

}

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

Page 201: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

case viewing

case sharing(dreams: [Dream])

case selecting(selectedRows: IndexSet)

// DreamListViewController — Isolating UI State

class DreamListViewController : UITableViewController {

...

}

enum State {

}

var isInViewingMode: Bool

var sharingDreams: [Dream]?

var selectedRows: IndexSet?

Page 202: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

// DreamListViewController — Isolating UI State

class DreamListViewController : UITableViewController {

var state: State

...

}

enum State {

case viewing

case sharing(dreams: [Dream])

case selecting(selectedRows: IndexSet)

}

Page 203: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Recap

Page 204: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Model View Controller

Controller

Model View

Page 205: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream View

Model View Controller

Page 206: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream View

CascadingLayout DecoratingLayout

Model View Controller

Page 207: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream ViewController.Model

CascadingLayout DecoratingLayout

Model View Controller

Page 208: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream ViewController.StateController.Model

CascadingLayout DecoratingLayout

Model View Controller

Page 209: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream View

Creature Effect

Controller.StateController.Model

CascadingLayout DecoratingLayout

InsetLayout

Model View Controller

Page 210: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Controller

Dream View

Creature Effect

Controller.StateController.Model

CascadingLayout DecoratingLayout

Model View Controller

InsetLayout

Page 211: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques and Tools

Page 212: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques and Tools

Customization through composition

Page 213: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques and Tools

Customization through compositionProtocols for generic, reusable code

Page 214: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques and Tools

Customization through compositionProtocols for generic, reusable codeTaking advantage of value semantics

Page 215: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Techniques and Tools

Customization through compositionProtocols for generic, reusable codeTaking advantage of value semanticsLocal reasoning

Page 216: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

More Information

https://developer.apple.com/wwdc16/419

Page 217: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller

Related Sessions

Understanding Swift Performance Mission Friday 11:00AM

Protocol-Oriented Programming in Swift WWDC 2015

Building Better Apps with Value Types in Swift WWDC 2015

Page 218: Protocol and Value Oriented Programming in UIKit Apps · Jacob Xiao Protocol Oriented Programmer Alex Migicovsky Swift Compiler Typo Engineer. Local Reasoning. Model View Controller