Upgrade to Pro — share decks privately, control downloads, hide ads and more …

從 UIKit 到 SwiftUI 的思維轉移

Pofat
October 23, 2021

從 UIKit 到 SwiftUI 的思維轉移

Pofat @ MOPCON 2021

UIKit 十多年來一直是 iOS app 開發者的主力 UI 框架,其基礎元件和 layout 工具都提供了多樣化的設定,以承擔 app 在這些年間不停變化的需求,但從 Swift 開始出現後,事情開始逐漸有了變化。

首先出現了利用 Swift 語言能力與性質提供了以往不同的 UIKit 開發典範。後來在 FRP 盛行數年後,2019 年 Combine 的到來宣告第一方的 FRP solution 終於出現,與此相伴的是基於 Combine 並帶來全新 app 開發體驗,在當年轟動了整個 Apple 開發生態系的 SwiftUI。

從 UIKit 到 SwiftUI ,其中開發的思維落差極大,讓我們一起來聊聊之中的差異以及 Apple 如何一步步地引導這個過程。

Pofat

October 23, 2021
Tweet

More Decks by Pofat

Other Decks in Programming

Transcript

  1. • Co-host @ [weak self] • Host @ Kóng Code

    • Swift lover • iOS developer I am Pofat
  2. • Set values to UI components and update them •

    Recalculate new frame size • Initialize new views. Show/hide existed views
  3. State UI Action State State State State State State State

    State UI UI UI UI UI UI UI UI UI UI UI Action Action Action Action Action
  4. Local Reasoning • Divide logic into sub domains • Only

    pay attention to current domain • One domain serves for one goal (ideally)
  5. View Life Cycle When To Update UI? View Management Layout

    Shared Mutable State Scalability State View
  6. Shared Mutable State Bad • Shared mutable state produces bugs

    • Values, can’t emphasize their importance more Shared Mutable State
  7. What Do We Care About Views? • Relationship to other

    views • Con fi guration • Layouts
  8. Layout Code Is Always There • Separate layout from views

    • Abstraction over views • [WWDC 16] Protocol And Value Oriented Programming In UIKit Apps
  9. Dive Into The Layout parentSize childSize1 layout(in: parentSize) childSize2 childSize3

    grandChildSize grandChildSize grandChildSize grandChildSize grandChildSize layout(in: childSizeN)
  10. What Do We Care About Views? • Relationship to other

    views • Con fi guration • Layouts
  11. What Do We Care About Views? • Relationship to other

    views • Con fi guration • Layouts
  12. Relationship To Other Views • How to describe them e

    ffi ciently? • How to do this in one place?
  13. What Do We Care About Views? • Relationship to other

    views • Con fi guration • Layouts
  14. What Do We Care About Views? • Relationship to other

    views • Con fi guration • Layouts
  15. How And When To Update UI • Gather multiple properties

    into a state for view presentation • Single code path to update UI • Whenever the state for view changes
  16. Update Path In SwiftUI • Only property changes are able

    to trigger view update • Update is triggered automatically • Only special properties @State, @ObservedObject, @ObservedObject, @Binding and @EnvironmentObject
  17. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View
  18. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View Single path
  19. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View Single path Top-down Layout
  20. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View Single path Top-down Layout DSL
  21. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View Single path Top-down Layout DSL Single Path
  22. SwiftUI View Life Cycle When To Update UI? View Management

    Layout Shared Mutable State Scalability State View Single path Top-down Layout DSL Single Path Value Types
  23. View = f(State) • App = View Relation + Layout

    + State + Actions(State mutation, side e ff ects) • Managing UI is actually managing your states (Ideally) Except Navigation!!
 For more code and deep dive: SwiftUI 導覽狀態探究
  24. Decomposition • One State For One App? TCA from Point-Free

    • Separated by developer, ViewModel
  25. Conclusion • Develop state as opposed to view • Tips

    for UIKit preparation: Extract layout logic, use UIStackView if possible and bind state-view in one place.