How to get all enum values in Swift?

So you made brand new and shiny enum, and want to use it in f.e. picker? To achieve this you need to prepare array of strings which every picker can use. How to do it?

It’s quite simple. All you have to do is add CaseIterable protocol to your enum. Like this:

Then in your ViewController add:

The last part is generating strings array from our enum values:

If you want to play around with this, grab XCode 10.2 Playground from my GitHub https://github.com/blastar/EnumPicker

Leave a Reply