Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set the type on enum in swift?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You have to access the rawValue of the enum case:

.cornerRadius(size.rawValue)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading