Slide 1

Slide 1 text

SwiftUI Lessons For UI/AppKit developers Federico Zanetello ★★★★★ fivestars.blog • @zntfdr

Slide 2

Slide 2 text

SwiftUI is not UI/AppKit 2

Slide 3

Slide 3 text

SwiftUI is not UIKit/AppKit 2 » Complete paradigm shift (imperative declarative) » We're all starting from scratch » Knowledge of earlier UI framework is: » " insightful » # not helpful » $ might even be a disadvantage (!!!)

Slide 4

Slide 4 text

SwiftUI Views are recipes

Slide 5

Slide 5 text

SwiftUI Views are recipes » where we declare how views look and behave... » ...but no longer manage transitions & similar » we give up full control on views

Slide 6

Slide 6 text

New way to make changes via state struct FSView: View { @State private var backgroundColor: Color = .white var body: some View { ZStack { backgroundColor.ignoresSafeArea() Button("Change background color") { backgroundColor = [.red, .white, .black] .filter { $0 != backgroundColor } .randomElement()! } } } }

Slide 7

Slide 7 text

New View communication ways » Bindings struct FSView: View { @State private var isOn = false var body: some View { Toggle( "Enable X", isOn: $isOn ) } }

Slide 8

Slide 8 text

It's expected to struggle when learning SwiftUI

Slide 9

Slide 9 text

The SwiftUI struggle » even simple things are going to be hard » all of us are still figuring it out » when people say something is impossible in SwiftUI, it means they haven’t figured it out yet » the struggle is real...until things will *just* click

Slide 10

Slide 10 text

Everything is a View, not every View is a view

Slide 11

Slide 11 text

Events are observed and delivered to views » onAppear » onDisappear » task » onReceive » onChange » onDrag » onDrop » onHover » onSubmit » …

Slide 12

Slide 12 text

Views can be containersα struct ContainerView: View { @StateObject var model = ContainerModel() var body: some View { ActualUI( elements: model.elements, onElementTap: model.onTap(element:) ) .onAppear(perform: model.onAppear) } } α https://swiftwithmajid.com/2019/07/31/introducing-container-views-in-swiftui/

Slide 13

Slide 13 text

SwiftUI coordinator architectureβ struct FlowCoordinator: View { @State private var routes: [Route] = [.root(.firstScreen)] var onCompletion: () -> Void var body: some View { Router($routes) { screen, _ in switch screen { case .firstScreen: FirstScreen(onCompletion: { routes.push(.secondScreen) }) case .secondScreen: SecondScreen(onCompletion: onCompletion) } } } } β https://github.com/johnpatrickmorgan/FlowStacks

Slide 14

Slide 14 text

SwiftUI is slow*

Slide 15

Slide 15 text

SwiftUI is slow* » Optimizations become implementation details » newer Xcode, faster app! ! » there's a limit to how much we can get for free » *SwiftUI is as performant as our code makes it so: » The more parameters/states/dependencies a view has, the less performance we might getγ » Isolate state as much as possibleδ » Publish only relevant changes » Make each view observe as little as possibleε ε https://github.com/cookednick/Observable/ δ https://www.wwdcnotes.com/notes/wwdc21/10022/ γ https://www.fivestars.blog/articles/app-state/

Slide 16

Slide 16 text

Feedback ⠀⠀⠀⠀⠀ Assistantλ ⠀⠀⠀⠀ ⠀ is your ⠀⠀ ⠀⠀ (new) friend ⠀ λ https://feedbackassistant.apple.com

Slide 17

Slide 17 text

Feedback Assistant pro tips » Be as narrow and concise as possible » File as early as possible » Follow-up when new SDKs are out » Add reproduction code (for bugs) » Bonus: attach a video demo » Describe your case (for suggestions)

Slide 18

Slide 18 text

UIKit/Appkit are not going anywhere

Slide 19

Slide 19 text

UIKit/Appkit are not going anywhere ! » SwiftUI uses both AppKit and UIKit behind the scenes » SwiftUI is just another tool in our belt » it's ok to mix and match! » UIViewRepresentable NSViewRepresentable » UIViewControllerRepresentable NSViewControllerRepresentable » UIHostingController NSHostingController » Feel free to experiment!

Slide 20

Slide 20 text

SwiftUI Lessons For UI/AppKit developers Federico Zanetello ★★★★★ fivestars.blog • @zntfdr