Skip to content

Instantly share code, notes, and snippets.

View mbrandonw's full-sized avatar

Brandon Williams mbrandonw

View GitHub Profile
/*
The @AppStorage property wrapper does not fully work unless installed
directly in a view. In particular, it does not work when used in an
observable object.
Paste the following into a fresh project, run it in the simulator,
and notice that tapping the "Toggle model.isOn" button causes the view
to update propertly, but tapping the "Toggle UserDefaults directly"
button does not. This means that writing directly to user defaults
does cause @AppStorage to update, _unless_ @AppStorage is used in the
struct SyncUpDetailView: View {
- @Bindable var model: SyncUpDetailModel
+ @Bindable var store: StoreOf<SyncUpDetail>
var body: some View {
Form {
Section {
Button {
- model.startMeetingButtonTapped()
+ store.send(.startMeetingButtonTapped)
@mbrandonw
mbrandonw / FB12969716.swift
Created December 2, 2023 15:01
FB12969716
# FB12969716
// FB: `Form` and
//
// Paste this into a fresh SwiftUI project's ContentView.swift to see a bug with `Form` views with
// conditional child views that use `@State`.
//
// 1. Tap "Toggle" to show the child view with a new model
// 2. Tap "+" in the child view to increment the model's state from 0 to another number
// 3. Tap "Toggle" two times to hide the child and then show it again
import SwiftUI
@Observable
class CounterModel {
var count = 0
}
struct CounterView: View {
let model: CounterModel
var body: some View {
Text(self.model.count.description)
@mbrandonw
mbrandonw / 66629.md
Last active August 16, 2023 11:09
WWDC 2023 Feedbacks

Cannot use #if canImport with @ObservationIgnored

@mbrandonw
mbrandonw / FB12101395.md
Last active April 6, 2023 19:19
Bug reports related to Swift's existential types.

Unbound memory growth when using existential with primary associated type in SwiftUI view

FB12101395, apple/swift issue #64974

Run the following code in a simulator, tap the “Go” button, and notice that the application freezes:

import SwiftUI

struct ContentView: View {
 var body: some View {
// A fix for https://github.com/AvdLee/TaskGroupsResultBuilder/blob/main/TaskGroups/TaskResultBuilder.swift
// to handle cancellation.
@resultBuilder
struct TaskBuilder {
static func buildExpression<Success: Sendable>(_ task: Task<Success, Never>) -> [Task<Success, Never>] {
[task]
}
static func buildBlock<Success: Sendable>(_ tasks: [Task<Success, Never>]...) -> [Task<Success, Never>] {

Automatically add -ObjC when linking static libraries that contain Swift or ObjC code

FB11788325

Is it possible for Xcode/linker to automatically link -ObjC when linking static libraries that contain Swift or ObjC code?

Without that it is possible for a protocol conformance in a static library to be seemingly stripped from the library. For example, if you have the following protocol hierarchy in a static library:

protocol P {}
@mbrandonw
mbrandonw / FB10492871.md
Created June 28, 2022 16:03
FB10492871: TextField writes to binding twice

TextField writes to binding twice

FB10492871

The binding provided to a TextField is written to twice for each keystroke.

Run the following code in a SwiftUI application and type into the text field to reproduce:

import SwiftUI

NotificationCenter.Notifications does not cooperatively cancel

FB10473067

When the following code is run in a playground or in an application:

let task = Task {
  for await _ in await NotificationCenter.default.notifications(named: UIApplication.userDidTakeScreenshotNotification) {
  }
 print(#line, "Sequence finished")