Share
Embed
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
Elvis Shi
August 30, 2025
Transcript
17 slides
-
!&OWJSPONFOU =LFZ1BUI ପહݺ ҂ᄍྸૌ҂ᆩ֡Ć f o r J 1M
BZ H S P V OE -5 -
} 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 { -
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) } } ࠎԤႨم -
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৵ࢲ 🤔 -
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৵ࢲ -
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) } } սೆԩ 🙅 -
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) } } սೆԩ 🤔 -
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) } } սೆԩ -
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 -
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