To pass Bool value in an enum variable of type someView

Hi guys I tried several times passing the bool value of my toogleswitch to the enum where all my cases are but its not working at all , I have tried to debug it for several hours but still its not working out

This is my ToggleView

import SwiftUI
struct ToggleView: View {
    @State private var isOn: Bool = false
    
    var body: some View {
        Toggle(isOn: $isOn) {
            Text("Toggle")
        }
    }
}

I want to pass isOn varaible to my BrailleLetters enum inside the enum there is var brailleLettersView where i want to access the boolean so I can use a ternary operator operator and change the ahapname: according to the boolean. Please if someone can help.

This is My Braille Letters Enum

import SwiftUI

enum BrailleLetters:CaseIterable {

    
    case A,B
    
    var brailleLettersView: some View {
        HStack{
        switch self {
        case .A :
            HStack {
                VStack {
               BrailleCircle(color: .red, hapticFeedback: .heavy,isFilled: true,ahapname:"Circle1")
                    BrailleCircle(color: .orange, hapticFeedback: .rigid,isFilled: false,ahapname: "_empty_")
                    BrailleCircle(color: .yellow, hapticFeedback: .medium,isFilled: false,ahapname: "_empty_")
                }
                VStack {
                    BrailleCircle(color: .green, hapticFeedback: .soft,isFilled: false,ahapname: "_empty_")
                    BrailleCircle(color: .blue, hapticFeedback: .light,isFilled: false,ahapname: "_empty_")
                    BrailleCircle(color: .purple, hapticFeedback: .medium,isFilled: false,ahapname: "_empty_")
                    
                }
            }
        case .B :
            HStack {
                VStack {
                    BrailleCircle(color: .red, hapticFeedback: .heavy,isFilled: true,ahapname: "Circle1")
                    BrailleCircle(color: .orange, hapticFeedback: .rigid,isFilled: true,ahapname: "Circle2")
                    BrailleCircle(color: .yellow, hapticFeedback: .medium,isFilled: false,ahapname: "_empty_")
                }
                VStack {
                    BrailleCircle(color: .green, hapticFeedback: .soft,isFilled: false,ahapname: "_empty_")
                    BrailleCircle(color: .blue, hapticFeedback: .light,isFilled: false,ahapname: "_empty_")
                    BrailleCircle(color: .purple, hapticFeedback: .medium,isFilled: false,ahapname: "_empty_")
                    
                }
            }
       
        }
      }
    }
  
}

I have not looked in depth, but I would try to use associated values to the case A and B.

Toggle is expecting a Binding<Bool>. There are many ways to build one. You’ve found one way: using $ to get the binding from the @State property. There are many others. One useful one is the init(get:set:) initialiser, which creates a binding from a getter and a setter. You can use this to do whatever tranforms you want. For example:

enum MyBool {
    case myTrue
    case myFalse
}

struct ContentView: View {
    @State private var isOn: MyBool = .myFalse
    var body: some View {
        Toggle("Test", isOn: .init(
            get: { self.isOn == .myTrue},
            set: { newValue in self.isOn = newValue ? .myTrue : .myFalse }
        ))
    }
}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@eskimo. I learned something here.

But I wonder about

newValue in self.isOn = newValue ? .myTrue : .myFalse 
  • why does the assignment yields true or false ?
  • what is its exact meaning ?

@Claude31

Equivalent closure:

{ newvalue in
    if newValue == true {
        self.isOn = .myTrue
    } else {
        self.isOn = .myFalse
    }
}

If it helps, think of everything that comes after the equal sign as a separate expression, like { newValue in self.isOn = (newValue ? .myTrue : .myFalse) }

To pass Bool value in an enum variable of type someView
 
 
Q