Slide 1

Slide 1 text

Value Types͔ΒݟΔWWDC16 ɹ WWDC16. Meetup @Wantedly with ೔ܦ৽ฉࣾ 2016/06/30 @shingt iOS / Rails Engineer @ Wantedly, Inc.

Slide 2

Slide 2 text

Value Types

Slide 3

Slide 3 text

WWDC 2016 → Protocol and Value Oriented Programming in UIKit Apps → What's New in Foundation for Swift → Understanding Swift Performance

Slide 4

Slide 4 text

Agenda * MVCΞϓϦέʔγϣϯʹ͓͚ΔValue Types * `Protocol and Value Oriented Programming in UIKit Apps` ΑΓ * Swift 3Ͱ௥Ճ͞ΕΔFoundation಺ͷValue Typesʹ͍ͭͯ * `What's New in Foundation for Swift` ΑΓ

Slide 5

Slide 5 text

MVCΞϓϦέʔγϣϯʹ͓͚ΔValue Types

Slide 6

Slide 6 text

WWDC 2015 → Protocol-Oriented Programming in Swift → Building Better Apps with Value Types in Swift

Slide 7

Slide 7 text

WWDC 2016 Protocol and Value Oriented Programming in UIKit Apps

Slide 8

Slide 8 text

Value Types in MVC-based Application * Model => ʢ࢖͑Δ৔ॴͰʣ࢖ͬͯ౰વʢUncontroversialʣ * View => ?ɹ * Controller => ? ɹ

Slide 9

Slide 9 text

Value Types in MVC-based Application * Model => ʢ࢖͑Δ৔ॴͰʣ࢖ͬͯ౰વʢUncontroversialʣ * View * StructͰͷϨΠΞ΢τॲཧ <= ࠓ೔঺հ͠·͢ * ΄͔ * Controller => ? ɹ

Slide 10

Slide 10 text

https://developer.apple.com/library/prerelease/ content/LucidDreams

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

class DecoratingLayoutCell : UITableViewCell { var content: UIView var decoration: UIView } // => UIViewʹ͸ద༻Ͱ͖ͳ͍

Slide 14

Slide 14 text

// CellͱUIViewͷͲͪΒʹ΋ద༻Ͱ͖ΔܗͰఆٛ struct DecoratingLayout { var content: UIView var decoration: UIView mutating func layout(in rect: CGRect) { content.frame = ... decoration.frame = ... } }

Slide 15

Slide 15 text

class DreamCell : UITableViewCell { override func layoutSubviews() { // DecoratingLayoutʹΑΓlayout var decoratingLayout = DecoratingLayout( content: content, decoration: decoration) decoratingLayout.layout(in: bounds) } }

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

struct DecoratingLayout { // contentͱdecorationΛಉ͡ܕʹ͢ΔͨΊͷ੍໿ΛGenericsͰ 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 {...}

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

ViewΛ૊Έ߹ΘͤΔʁ

Slide 20

Slide 20 text

Composition of Views "Minimize the number of views that we use."ɹɹ ʢViewͷੜ੒͸ίετߴͳͷͰʣ => Composition of Values

Slide 21

Slide 21 text

Composition of values

Slide 22

Slide 22 text

Composition of values struct CascadingLayout { // Layoutʹద߹ͨ͠ܕͷ഑ྻ var children: [Child] mutating func layout(in rect: CGRect) {...} }

Slide 23

Slide 23 text

struct MultiPaneLayout<...>: Layout { mutating func layout(in rect: CGRect) { // CascadingLayoutͱDecoratingLayoutΛ૊Έ߹ΘͤΔ let decoration = CascadingLayout(children: accessories) var composedLayout = DecoratingLayout( content: content, decoration: decoration) // ࠷ऴతͳϨΠΞ΢τॲཧ composedLayout.layout(in: rect) } }

Slide 24

Slide 24 text

ηογϣϯͰ͸͜͜·Ͱ => αϯϓϧίʔυΛ೷͘ͱ

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

؆қ·ͱΊ * Viewɹ * StructΛ࢖ͬͨϨΠΞ΢τॲཧ * Value TypesʹΑΔComposition * Controllerʢলུʣ * Single code pathɹ * EnumͰͷঢ়ଶ؅ཧ * શମͱͯ͠Local Reasoningʢہॴਪ࿦ʣՄೳͳίʔυ

Slide 27

Slide 27 text

Swift 3Ͱ௥Ճ͞ΕΔFoundation಺ͷ Value Typesʹ͍ͭͯ

Slide 28

Slide 28 text

→ What's New in Foundation for Swift

Slide 29

Slide 29 text

0069-swift-mutability-for-foundation.md

Slide 30

Slide 30 text

Swift 3ͰͷFoundationͷมԽ * FoundationͷҰ෦ͷReference TypesΛϥοϓ͢ΔValue Types͕௥Ճ * ྫ: NSDate => Date * "mutability when you need it"ɹ * ඪ४ϥΠϒϥϦͷprotocol΁ͷ४ڌ * ྫ: Equatable, Comparable, ... * ܕηʔϑͷՕॴ͕૿͑Δ * NSϓϨϑΟΫε͕औΕΔʢ͜Ε͸ผͷproposalʣ

Slide 31

Slide 31 text

Ұ෦

Slide 32

Slide 32 text

ྫ: Date public struct Date : ReferenceConvertible, Comparable, Equatable, CustomStringConvertible { ... } // Comparable public func <(lhs: Date, rhs: Date) -> Bool { return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate }

Slide 33

Slide 33 text

func whenToLeave() -> Date { ... } let when = whenToLeave().addingTimeInterval(-5.0 * 60.0) // Comparableʹ४ڌ͍ͯ͠ΔͨΊ if Date() < when { // Կؾʹselectorແ͠Ͱهड़Ͱ͖ΔΑ͏ʹͳ͍ͬͯΔTimer timer = Timer(fireDate: when, interval: 0, repeats: false) { print("Almost time to go!") } RunLoop.main.add(timer, forMode: .commonModes) }

Slide 34

Slide 34 text

ηογϣϯதͷଞͷ࿩୊ * ͦͷଞͷ֤छAPIͷมߋ಺༰ * SwiftͱObjective-CؒͷBridging * Swift 3༻API΁ͷMigration

Slide 35

Slide 35 text

ͱ͜ΖͰValue Types͸ * ͲͷΑ͏ͳ࣌ʹར༻͢΂͖ͳͷ͔ʁ * ͦͷ࣌ͳͥύϑΥʔϚϯε͕ྑ͍ͷ͔ʁ

Slide 36

Slide 36 text

→ Understanding Swift Performance (WWDC 16) → Optimizing Swift Performance (WWDC 15) * Memory allocation * Reference counting * Method dispatch ͳͲͷ؍఺͔Β಺෦࣮૷΋ަ͑ͯղઆ

Slide 37

Slide 37 text

Summary * WWDC 16ͰͷValue Typesʹؔͯ͠ͷ࿩୊Λ঺հ͠·ͨ͠ * MVCΞϓϦέʔγϣϯʹ͓͚ΔValue Typesͷར༻ * StructͷCompositionͰLayoutΛදݱ͢Δྫͷ঺հ * Swift 3Ͱ௥Ճ͞ΕΔFoundation಺ͷValue Typesʹ͍ͭͯ * Dateͷྫͷ঺հ

Slide 38

Slide 38 text

Reference * Value and Reference Types - Swift Developer Blog * https://developer.apple.com/swift/blog/?id=10 * WWDC 15 * Building Better Apps with Value Types in Swift * https://developer.apple.com/videos/play/wwdc2015/414/ * Protocol-Oriented Programming in Swift * https://developer.apple.com/videos/wwdc/2015/408/ * Optimizing Swift Performance * https://developer.apple.com/videos/play/wwdc2015/409/ * WWDC 16 * Protocol and Value Oriented Programming in UIKit Apps * https://developer.apple.com/videos/play/wwdc2016/419/ * What's New in Foundation for Swift * https://developer.apple.com/videos/play/wwdc2016/207/ * Understanding Swift Performance * https://developer.apple.com/videos/play/wwdc2016/416/ * https://github.com/apple/swift-corelibs-foundation