Skip to content

Instantly share code, notes, and snippets.

View mbrandonw's full-sized avatar

Brandon Williams mbrandonw

View GitHub Profile
import SwiftUI
class Counter {
var count = 0
}
extension EnvironmentValues {
@Entry var counter = Counter()
}
struct ContentView: View {
/*
The following feature is 100% pure Swift code. It compiles for a multitude of platforms,
include iOS, macOS, Linux, Windows, Wasm, and more. It encapsulates all of the logic
and behavior for the feature so that the view can concentrate on just the UI by reading
state from the model and invoking methods on the model.
*/
import Dependencies
import FactClient
import Perception
/*
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 {}