Home > Mobile >  Value of type 'CalcButton' has no member 'rawValue'
Value of type 'CalcButton' has no member 'rawValue'

Time:04-17

I have a problem. I am a beginner in programming and searched for how to make a calculator on youtube and tried to do it my self. I do exactly as he did but i get an error message and can't figure out what the problem is and have searched all over the forum for help but don't see any answers! I'm using Xcode on my macbook and are using swift.

here is the code:


enum CalcButton {
    case one
    case two
    case three
    case four
    case five
    case six
    case seven
    case eight
    case nine
    case zero
    case dividera
    case multiplicera
    case subtrahera
    case addera
    case likamed
    case clear
    case procent
    case plusminus
}

struct ContentView: View {
    
    let buttons: [[CalcButton]] = [
        [.seven, .eight, .nine]
    ]
    
    var body: some View {
        ZStack {
            Color.black.edgesIgnoringSafeArea(/*@START_MENU_TOKEN@*/.all/*@END_MENU_TOKEN@*/)
            VStack {
                
                // Text Display
                HStack {
                    Spacer()
                    Text("0")
                        .bold()
                        .font(.system(size: 64))
                        .foregroundColor(.white)
                    
                    // Knappar
                    
                    ForEach(buttons, id: \.self) { row in
                        ForEach(row, id: \.self) { item in
                            Button(action: {
                                
                                
                                   }, label: {
                                    Text(item.rawValue)
                                   
                                   
                            
                        
                        })
                    }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
}```

CodePudding user response:

In your code you are calling item.rawValuebut your enum CalcButtons has no raw value associated with it so this property doesn’t exists for the enum.

You need to assign a raw value and judging by how it is used it should be String so change your enum declaration to

enum CalcButtons: String { 
    <same code as before>
}

You can read more about enum raw values here but in short what happens is that each case item in your enum gets a string value assigned to it that is the same as the item name.

  • Related