Estou tentando passar um valor @Binding para a hierarquia de visualizações do SwiftUI usando uma @EnvironmentKey personalizada. Na minha NestedView, estou usando uma combinação de @Environment e @Binding como esta:
@Environment(\.featureEnabledBinding) @Binding var myBool: Bool
Eu deveria conseguir usar isso em um Toggle como:
Toggle(isOn: $myBool, label: { Text("Toggle") })
Entretanto, isso me dá um erro do compilador:
Não é possível encontrar '$myBool' no escopo
isso funciona perfeitamente:
Toggle(isOn: _myBool.wrappedValue, label: { Text("Toggle") })
Isso é o oposto do que eu esperava. Pelo que entendi: • _myBool deve ser o wrapper (Binding) • $myBool deve ser o valor projetado (também um Binding)
Aqui está meu código
Configuração do ambiente:
struct FeatureEnabledKey: EnvironmentKey {
static let defaultValue: Binding<Bool> = .constant(false)
}
extension EnvironmentValues {
var featureEnabledBinding: Binding<Bool> {
get { self[FeatureEnabledKey.self] }
set { self[FeatureEnabledKey.self] = newValue }
}
}
e Exemplo Mínimo
struct ParentView: View {
@State private var isFeatureEnabled = false
var body: some View {
NestedView()
.environment(\.featureEnabledBinding, $isFeatureEnabled)
}
}
struct NestedView: View {
@Environment(\.featureEnabledBinding) @Binding var myBool: Bool
var body: some View {
VStack {
// ❌ This gives an error : Cannot find '$myBool' in scope
// Toggle(isOn: $myBool, label: { Text("Toggle") })
// ✅ This works fine
Toggle("Working Toggle", isOn: _myBool.wrappedValue)
}
}
}
Pergunta: Por que $myBool está indisponível ou inválido neste contexto? É um bug conhecido do SwiftUI/compilador com wrappers de propriedade dupla (@Environment @Binding)? Ou meu entendimento não está correto.
Xcode: 16.3