Skip to content

Instantly share code, notes, and snippets.

import Foundation
struct SomeObject: Codable {
let name: String
}
struct OtherObject: Codable {
let number: Int
}
enum Color {
case red
}
struct carsA {
let ofColor: Color
}
let carsA = carsA(ofColor: .red) // error: Circular reference
import SwiftUI
protocol Selectable: Identifiable {
associatedtype Label : View
var isSelected: Bool { get set }
var label: Label { get }
}
struct Multiselect<T: Selectable>: View {
@Binding var items: [T]
import SwiftUI
struct Child: View {
@Binding var binding: Int
var body: some View {
VStack {
Text("x = \(binding)")
Button("Mutate parent view .x") {
binding += 1 // this will cause a new child view push onto the nav stack
import SwiftUI
struct Child: View {
@Binding var binding: Int
var body: some View {
VStack {
Text("x = \(binding)")
Button("Mutate parent view .x") {
binding += 1 // this will cause various different nav stack push pop problem
import SwiftUI
struct ContentView: View {
@AppStorage("number") var number = 1
var body: some View {
VStack {
Text("Hello, world! number = \(number)")
.padding()
Button("+1") {
import SwiftUI
/// Wraps a view to be loaded on-demand when using a `NavigationLink`
/// This helps to increate performance as less of the graph is loaded initially.
struct NavigationLazyView<Content: View>: View {
let build: () -> Content
// as-is, compile error at call site
init(_ build: @autoclosure @escaping () -> Content) {
// !!! had to mark as @ViewBuilder here
// init(@ViewBuilder _ build: /*@autoclosure*/ @escaping () -> Content) {
extension View {
/// Workaround .foregroundColor(nil) not inherit from outter view bug
///
/// ```
/// ZStack {
/// Image(systemName: "star.fill")
/// .conditional(foregroundColor: Int.random(in: 1...5) < 2 ? .green : nil)
/// }
/// .foregroundColor(.red)
/// ```
import SwiftUI
public protocol ObservablePropertySource: ObservableObject {
static var shared: Self { get }
}
@propertyWrapper
public struct ObservableProperty<S: ObservablePropertySource, T>: DynamicProperty {
@ObservedObject private var source: S
import SwiftUI
private struct OnFirstAppear: ViewModifier {
let perform: () -> Void
@State private var firstTime = true
func body(content: Content) -> some View {
content.onAppear {
if firstTime {