Enums - storing values and their keys

1. Right click the main folder of your project (for instance, calculator folder with the folder icon at the side) and add a new swift file. You can create a swift file to hold all your enums and structures.

Type:

enum Operator: String
{
 case Add = "+"
 case Subtract = "-"

)

I can select Operator.Add and I will get "+"
This is useful when you need a central repository to store strings so that when you edit it once, the rest will reflect the change.

To access it in the view controller, you can use it to create an object:

var cOpt: Operator = Operator.Add

Comments

Popular posts from this blog

Setting up a playground

Go to another page