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鈥檓 using the new containerRelativeFrame modifier to size these park cards relative to the visible size of the horizontal scroll view. I鈥檇 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鈥檛 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鈥檚 the difference between a custom ViewModifier vs View extension Q: What鈥檚 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鈥檚 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鈥檚 the difference between a custom ViewModifier vs View extension Q: What鈥檚 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鈥檚 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鈥檚 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鈥檚 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 鈥渟ome Animal鈥. The 鈥渟ome鈥 in 鈥渟ome Animal鈥 indicates that there is a specific type that you鈥檙e 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鈥檚 New in Swift String Generic Codable Advanced Animations with UIKit Multiple animation Interactive animation Natural Language Processing and your Apps NSLinguisticTagger What鈥檚 New in Cocoa Touch Large title Drag and drop File management Safe area What鈥檚 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鈥檚 New in Testing Parallel testing Wait Screenshot Multiple app scenario

June 10, 2017 路 1 min 路 Khoa Pham