Value Types in WWDC16

Value Types in WWDC16

#wwdc16_meetup

3652e140db258288623a0adf248baec0?s=128

Shinichi Goto

June 30, 2016
Tweet

Transcript

  1. 3.

    WWDC 2016 → Protocol and Value Oriented Programming in UIKit

    Apps → What's New in Foundation for Swift → Understanding Swift Performance
  2. 4.

    Agenda * MVCΞϓϦέʔγϣϯʹ͓͚ΔValue Types * `Protocol and Value Oriented Programming

    in UIKit Apps` ΑΓ * Swift 3Ͱ௥Ճ͞ΕΔFoundation಺ͷValue Typesʹ͍ͭͯ * `What's New in Foundation for Swift` ΑΓ
  3. 9.

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

    View * StructͰͷϨΠΞ΢τॲཧ <= ࠓ೔঺հ͠·͢ * ΄͔ * Controller => ? ɹ
  4. 11.
  5. 12.
  6. 14.

    // CellͱUIViewͷͲͪΒʹ΋ద༻Ͱ͖ΔܗͰఆٛ struct DecoratingLayout { var content: UIView var decoration:

    UIView mutating func layout(in rect: CGRect) { content.frame = ... decoration.frame = ... } }
  7. 15.

    class DreamCell : UITableViewCell { override func layoutSubviews() { //

    DecoratingLayoutʹΑΓlayout var decoratingLayout = DecoratingLayout( content: content, decoration: decoration) decoratingLayout.layout(in: bounds) } }
  8. 16.
  9. 17.

    struct DecoratingLayout<Child: Layout> { // 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 {...}
  10. 18.
  11. 20.

    Composition of Views "Minimize the number of views that we

    use."ɹɹ ʢViewͷੜ੒͸ίετߴͳͷͰʣ => Composition of Values
  12. 22.

    Composition of values struct CascadingLayout<Child : Layout> { // Layoutʹద߹ͨ͠ܕͷ഑ྻ

    var children: [Child] mutating func layout(in rect: CGRect) {...} }
  13. 23.

    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) } }
  14. 25.
  15. 26.

    ؆қ·ͱΊ * Viewɹ * StructΛ࢖ͬͨϨΠΞ΢τॲཧ * Value TypesʹΑΔComposition * Controllerʢলུʣ

    * Single code pathɹ * EnumͰͷঢ়ଶ؅ཧ * શମͱͯ͠Local Reasoningʢہॴਪ࿦ʣՄೳͳίʔυ
  16. 30.

    Swift 3ͰͷFoundationͷมԽ * FoundationͷҰ෦ͷReference TypesΛϥοϓ͢ΔValue Types͕௥Ճ * ྫ: NSDate =>

    Date * "mutability when you need it"ɹ * ඪ४ϥΠϒϥϦͷprotocol΁ͷ४ڌ * ྫ: Equatable, Comparable, ... * ܕηʔϑͷՕॴ͕૿͑Δ * NSϓϨϑΟΫε͕औΕΔʢ͜Ε͸ผͷproposalʣ
  17. 31.
  18. 32.

    ྫ: Date public struct Date : ReferenceConvertible, Comparable, Equatable, CustomStringConvertible

    { ... } // Comparable public func <(lhs: Date, rhs: Date) -> Bool { return lhs.timeIntervalSinceReferenceDate < rhs.timeIntervalSinceReferenceDate }
  19. 33.

    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) }
  20. 36.

    → Understanding Swift Performance (WWDC 16) → Optimizing Swift Performance

    (WWDC 15) * Memory allocation * Reference counting * Method dispatch ͳͲͷ؍఺͔Β಺෦࣮૷΋ަ͑ͯղઆ
  21. 38.

    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