Home > Software design >  How to Declare an Enum data type Binded Variable in SwiftUI?
How to Declare an Enum data type Binded Variable in SwiftUI?

Time:08-05

I was trying to bind an enum data type to a variable but its giving me error given bellow:

Cannot infer contextual base in reference to member

No exact matches in call to initializer

So my question is How can i declare an enum type Binded value in Swift UI?

I am using xcode of Version 13.4.1 (13F100)

My Codes are:

    enum BackLightStatus {
    case available
    case notAvalable
    case byDefault
}

import SwiftUI

struct KeyboardBackLightMainView: View {
    @State var isBackLightAvailable:BackLightStatus = .byDefault
    @State var backlightFlashing:Bool = false
    func fireTimer() {
        print("Timer fired!")
    }
    var body: some View {
        
        NavigationView {
            keyboardBackLightTestButton()
            KeyboardBackLightResultView(isKeyboardBackLightAvailable: $isBackLightAvailable)
        }
        .onDisappear(){
            backlightFlashing = false
            Backlight.sharedBacklight.stopFlashing()
            
        }
        
    }
    
    
}



struct KeyboardBackLightResultView: View {
    
    @Binding var isKeyboardBackLightAvailable : BackLightStatus = .byDefault // Error warning pops up here in xcode
    var body: some View {
        
        switch isKeyboardBackLightAvailable {
        case .available:
            Text("Keyboard BackLight is available and started to Flash. ")
                .padding()
                .font(.system(size: 16))
        case .notAvalable:
            Text("Keyboard BackLight is not available")
                .padding()
                .font(.system(size: 16))
                .foregroundColor(.red)
        case .byDefault:
            EmptyView()
        }
    }
}

enter image description here

CodePudding user response:

The @Binding wrapped property is just declared, w/o default, like

struct KeyboardBackLightResultView: View {
    
    @Binding var isKeyboardBackLightAvailable : BackLightStatus  // << here !!
// ...
  • Related