This file contains 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 User { | |
var firstName: String | |
var lastName: String | |
var email: String | |
var phoneNumber: String | |
var street: String | |
var city: String | |
var state: String | |
var zipCode: String | |
} |
This file contains 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 Account: Identifiable { | |
let id = UUID().uuidString | |
} | |
struct SheetLaunchingDemoView: View { | |
@State var showSheet: Account? | |
var body: some View { | |
NavigationStack { | |
Button("Show Sheet") { | |
showSheet = Account() |
This file contains 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 InterruptedTaskViewModel: ObservableObject { | |
@Published var name = "Michael" | |
init() { | |
print("INIT") | |
} | |
@MainActor | |
func load() { | |
Task { | |
print("LOADING") | |
let _ = try? await Task.sleep(nanoseconds: 3 * NSEC_PER_SEC) |
This file contains 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 ChildStateDemo: View { | |
@State var account = 1 | |
var body: some View { | |
VStack(spacing: 20) { | |
SubView(id: account) | |
.id(account) // required to change subview state | |
Button("Account 1") { | |
account = 1 | |
} |
This file contains 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 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 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 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 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 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 | |
} | |
} | |
} |
NewerOlder