WWDC23 SwiftUI Q&A

Issue #918 Interesting SwiftUI Q&A during WWDC23 Observable vs ObservableObject Q: With the new SwiftUI @Observable macro, are there any cases where ObservableObject would still be a better alternative? A: Use ObservableObject when you need to back deploy to before iOS 17 A: SwiftUI registers dependencies is a value is read during the evaluation of body. Indirect modifications should invalidate the dependencies. containerRelativeFrame in ScrollView Q: For the containerRelativeFrame, does that work cleanly in a List as well as a ScrollView?...

June 12, 2023 · 27 min · Khoa Pham

What's new in SwiftUI iOS 17 at WWDC23

Issue #916 WWDC23 brings new additions to SwiftUI Scrolling The scroll transition modifier is very similar to the visual effect modifier Curt used earlier for the welcome screen. It lets you apply effects to items in your scroll view. I’m using the new containerRelativeFrame modifier to size these park cards relative to the visible size of the horizontal scroll view. I’d like my park cards to snap into place....

June 7, 2023 · 13 min · Khoa Pham

How to use actor in Swift concurrency

Issue #905 Protect mutable state with Swift actors Actor reentrancy Imagine we have two different concurrent tasks trying to fetch the same image at the same time. The first sees that there is no cache entry, proceeds to start downloading the image from the server, and then gets suspended because the download will take a while. While the first task is downloading the image, a new image might be deployed to the server under the same URL....

October 5, 2022 · 4 min · Khoa Pham

How Task use thread in Swift concurrency

Issue #904 Consider this code where we have an ObservableObject with fetch1 and async fetch2, and a fetch inside ContentView Here the observation in Xcode 14 ViewModel.fetch1: run on main thread ViewModel.fetch2: run on cooperative thread pool ContentView.fetch: run on main thread import SwiftUI import CoreData import Combine class ViewModel: ObservableObject { @Published var string = "" func fetch1() { let url = URL(string: "https://google.com")! let data = try!...

October 4, 2022 · 4 min · Khoa Pham

Essential WWDC sample codes

Issue #892 WWDC21 Building a Great Mac App with SwiftUI Add Rich Graphics to Your SwiftUI App

June 10, 2022 · 1 min · Khoa Pham

My favorite WWDC videos

Issue #891 Below are my favorite WWDC videos. My focus is to use SwiftUI to make useful apps with great UX. I don’t pay much attention to new frameworks as they come and go, but the underlying reasons and design principles are worth remembering. WWDC23 The SwiftUI cookbook for focus Discussing some of the things that you can do with focus APIs in SwiftUI to cook up a really great user experience...

June 10, 2022 · 5 min · Khoa Pham

WWDC swiftui-lounge

Issue #890 In WWDC21, WWDC22 Apple provide a Slack channel https://wwdc22.slack.com/ for people to interact with Apple engineers in digital lounges. Here I note down some interesting Q&As WWDC22 What’s the difference between a custom ViewModifier vs View extension Q: What’s the difference between a custom ViewModifier (without DynamicProperty) that uses some built-in modifiers in body(content:), and a custom View extension func that just use those built-in modifiers? Similarly, what’s the difference between a custom ViewModifier with some DynamicProperty and a custom View with some DynamicProperty (also has a @ViewBuilder content property to receive content to modify) ?...

June 10, 2022 · 32 min · Khoa Pham

WWDC22 SwiftUI Q&A

Issue #890 Interesting SwiftUI Q&A during WWDC22 What’s the difference between a custom ViewModifier vs View extension Q: What’s the difference between a custom ViewModifier (without DynamicProperty) that uses some built-in modifiers in body(content:), and a custom View extension func that just use those built-in modifiers? Similarly, what’s the difference between a custom ViewModifier with some DynamicProperty and a custom View with some DynamicProperty (also has a @ViewBuilder content property to receive content to modify) ?...

June 10, 2022 · 25 min · Khoa Pham

What's new in SwiftUI iOS 16 at WWDC22

Issue #889 What’s new in SwiftUI New EnvironmentValues TextField inside Alert List uses UICollectionView See gist https://gist.github.com/onmyway133/fc08111964984ef544a176a6e9806c18 ButtonStyle composition Section("Hashtags") { VStack(alignment: .leading) { HStack { Toggle("#Swiftastic", isOn: $swiftastic) Toggle("#WWParty", isOn: $wwdcParty) } HStack { Toggle("#OffTheCharts", isOn: $offTheCharts) Toggle("#OneMoreThing", isOn: $oneMoreThing) } } .toggleStyle(.button) .buttonStyle(.bordered) } Customize Charts struct MonthlySalesChart: View { var body: some View { Chart(data, id: \.month) { BarMark( x: .value("Month", $0.month, unit: .month), y: .value("Sales", $0.sales) ) } ....

June 9, 2022 · 5 min · Khoa Pham

How to use any vs some in Swift

Issue #888 Embrace Swift generics This generic pattern is really common, so there’s a simpler way to express it. Instead of writing a type parameter explicitly, we can express this abstract type in terms of the protocol conformance by writing “some Animal”. The “some” in “some Animal” indicates that there is a specific type that you’re working with. func feed<A>(animal: A) where A: Animal {} func feed(animal: some Animal) An abstract type that represents a placeholder for a specific concrete type is called an opaque type....

June 8, 2022 · 5 min · Khoa Pham

Favorite WWDC 2017 sessions

Issue #56 Source WWDC 2017 Introducing Core ML Core ML Introducing ARKit: Augmented Reality for iOS ARKit What’s New in Swift String Generic Codable Advanced Animations with UIKit Multiple animation Interactive animation Natural Language Processing and your Apps NSLinguisticTagger What’s New in Cocoa Touch Large title Drag and drop File management Safe area What’s New in Foundation KeyPath Observe Codable Debugging with Xcode 9 Wireless debugging View controller debugging Core ML in depth Model Core ML tools Vision Framework: Building on Core ML Detection Track What’s New in Testing Parallel testing Wait Screenshot Multiple app scenario

June 10, 2017 · 1 min · Khoa Pham