This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ParentView: View { | |
@State var id = UUID().uuidString | |
var body: some View { | |
VStack(spacing: 20) { | |
VStack { | |
Text("Parent View") | |
Text(id).font(.footnote) | |
} | |
ChildView(name: "Child Maintains State On Update") | |
ChildView(name: "Child Loses State On Update") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
class ContentModel: ObservableObject { | |
@Published var count: Int = 0 | |
let items10 = (0 ..< 10).map { Item(id: $0 + 1) } | |
let items100 = (0 ..< 100).map { Item(id: $0 + 1) } | |
let items10K = (0 ..< 10_000).map { Item(id: $0 + 1) } | |
let items100K = (0 ..< 100_000).map { Item(id: $0 + 1) } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Classic | |
class ShopViewModel: ObservableObject { | |
@Published private(set) var products: [Products] | |
init(products: [Products] = []) { | |
self.products = products | |
} | |
public func add(product) { | |
products.append(product) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DeinitTracker {} | |
class Test { | |
init() {} | |
#if DEBUG | |
lazy var deinitTracker: DeinitTracker? = DeinitTracker() | |
#endif | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Binding { | |
public func defaultValue<T>(_ value: T) -> Binding<T> where Value == Optional<T> { | |
Binding<T> { | |
wrappedValue ?? value | |
} set: { | |
wrappedValue = $0 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Binding { | |
public static func variable(_ value: Value) -> Binding<Value> { | |
var state = value | |
return Binding<Value> { | |
state | |
} set: { | |
state = $0 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tableViewDataSource = users | |
.filter { $0.age > 21 && $0.name != "Simon" && $0.phone.contains("iPhone") } | |
.map { ViewModel(cellTitle: $0.name, cellSubtitle: $0.phone) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RingEditViewModel: ObservableObject { | |
@NestedObservableObject var ring: RingData | |
private let isEditing: Bool | |
private let temporaryContext: NSManagedObjectContext | |
init(context: NSManagedObjectContext, ring: RingData? = nil) { | |
self.temporaryContext = context.child() | |
if let ring = temporaryContext.copy(of: ring) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Combine+Extensions.swift | |
// Common | |
// | |
// Created by Michael Long on 8/27/22. | |
// | |
import Foundation | |
import Combine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ToDo: Codable, Identifiable { | |
let id: Int | |
let userId: Int | |
var title: String | |
var completed: Bool | |
} | |
class SelectionManager<Item: Identifiable>: ObservableObject { | |
@Published var status: [Item.ID:Bool] = [:] | |
func isSelected(_ item: Item) -> Bool { |