Upgrade to Pro — share decks privately, control downloads, hide ads and more …

@Environment(\.keyPath)那么好我不允许你们不知道! / atEnviro...

@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!

Avatar for Elvis Shi

Elvis Shi

August 30, 2025
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. } var employedBy = "YUMEMI Inc." var job = "iOS

    Developer" var organizerOf = "HAKATA.swift" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ ߎી༝ܸ෻ຖݓეđၛᇀႿᄝੌ༯֥FamilyMart୤ޱࣕग़ “౨໙গčlaʣ䭓ʢjiʣԳࡏ䬟ཬ” """ final class Me: Developable, Talkable {
  2. extension EnvironmentValues { @Entry var count: Int = 0 }

    struct CountView: View { @Environment(\.count) var count var body: some View { Text("Count: \(count)") } } struct ContentView: View { @State private var count = 0 var body: some View { VStack { CountView() // ... } .environment(\.count, count) } } ࠎԤႨم
  3. extension EnvironmentValues { @Entry var count: Int = 0 }

    struct CountView: View { @Environment(\.count) var count var hash: Int { count.hashValue } var body: some View { Text("Count: \(hash)") } } struct ContentView: View { @State private var count = 0 var body: some View { VStack { CountView() // ... } .environment(\.count, count) } } ,FZ1BUI৵ࢲ 🤔
  4. extension EnvironmentValues { @Entry var count: Int = 0 }

    struct CountView: View { @Environment(\.count.hashValue) var hash var body: some View { Text("Count: \(hash)") } } struct ContentView: View { @State private var count = 0 var body: some View { VStack { CountView() // ... } .environment(\.count, count) } } ,FZ1BUI৵ࢲ
  5. extension EnvironmentValues { @Entry var count: Int = 0 }

    struct CountView: View { @Environment(\.count) var count var body: some View { VStack { // ... Button("Increment") { count += 1 } } } } struct ContentView: View { @State private var count = 0 var body: some View { CountView() .environment(\.count, count) } } սೆԩ৘ 🙅
  6. extension EnvironmentValues { @Entry var _count: Binding<Int> = .constant(0) var

    count: Int { _count.wrappedValue } } struct CountView: View { @Environment(\.count) var count var body: some View { VStack { // ... Button("Increment") { _count.wrappedValue += 1 } } } } struct ContentView: View { @State private var count = 0 var body: some View { CountView() .environment(\._count, $count) } } սೆԩ৘ 🤔
  7. extension EnvironmentValues { @Entry var _count: Binding<Int> = .constant(0) var

    count: Int { _count.wrappedValue } } struct CountUpAction { fileprivate var action: () -> Void func callAsFunction() { action() } } extension EnvironmentValues { var countUp: CountUpAction { .init { _count.wrappedValue += 1 } } } struct CountView: View { @Environment(\.count) var count @Environment(\.countUp) var countUp var body: some View { VStack { // ... Button("Increment") { countUp() } } } } struct ContentView: View { @State private var count = 0 var body: some View { CountView() .environment(\._count, $count) } } սೆԩ৘
  8. struct CountUpAction { fileprivate var action: () -> Void func

    callAsFunction() { action() } } extension EnvironmentValues { var countUp: CountUpAction { .init { _count.wrappedValue += 1 } } } struct CountView: View { // ... @Environment(\.countUp) var countUp var body: some View { VStack { Text("Count: \(count)") .foregroundStyle(color.title) Button("Increment") { countUp() } } } } #Preview { @Previewable @State var previewCount: Int = 0 CountView() .environment(\._count, $previewCount) .environment(\.countUp, .init(action: { /* ... */ })) } 🙅 ᇿೆીႵ4FUUFS֥&OW
  9. struct CountUpAction { fileprivate var action: () -> Void func

    callAsFunction() { action() } } extension EnvironmentValues { var countUp: CountUpAction { .init { _count.wrappedValue += 1 } } } struct CountView: View { // ... @Environment(\.countUp) var countUp var body: some View { // ... } } private extension EnvironmentValues { var previewAction: CountUpAction { .init { print("Preview Count Up!") _count.wrappedValue += 100 } } } #Preview { @Previewable @State var previewCount: Int = 0 CountView(countUp: .init(\.previewAction)) .environment(\._count, $previewCount) } ᇿೆીႵ4FUUFS֥&OW