Home > Software engineering >  How to set the type on enum in swift?
How to set the type on enum in swift?

Time:05-11

I am attempting to create a property on a struct that accepts a size param (small, med, large) where each size enumerates a CGFloat value to be used in a modifier.

Struct CoolView: View {

enum Size: CGFloat {    
        case large = 100
        case medium = 200
        case small = 400
    }

let size: Size = .small

  var body: some View {
        ZStack() {
            Color.green
                .cornerRadius(size)

I get the error "Cannot convert value of type 'CoolView.Size' to expected argument type 'CGFloat?' yet I am setting enum Size: CGFloat

CodePudding user response:

You have to access the rawValue of the enum case:

.cornerRadius(size.rawValue)

  • Related