Home > Net >  Getting error : Expected expression in list of expressions
Getting error : Expected expression in list of expressions

Time:04-01

Can i not use switch to check for a variable and implement a view based on the variable's value. I tried using if else as well but still getting the same error. Do I have to create a method and return a view for the same and use it here?

struct AppThemeButton: View {

    var action: (() -> Swift.Void)?
    var buttonType: ThemeButtonType = .bordered

    var body: some View {
        Button {
            // button action
            if let act = action {
                act()
            }
        
        } label: {
            Text("  \(TextStrings.addAProject.localized())")
                .frame(maxWidth: .infinity, maxHeight: .infinity, 
alignment: .center)
                .background(
                    switch self.buttonType {
                    case .bordered:
                        Color.green
                    case .colored:
                        Color.red
                    }
                )
                .frame(height: 60, alignment: .center)
                .padding([.leading, .trailing])
        }
    }
}

enum ThemeButtonType {
    case bordered
    case colored
}

Code with error

CodePudding user response:

You're using this modifier enter image description here

  • Related