Associated Values

You will often want to associate a value with a member of an enum. Having the member itself is helpful, but sometimes you need more information. Here’s how you could create a Computer enum to get an idea of what I mean:

enum Computer {    //ram and processor    case Desktop(Int,String)    //screen size, model    case Laptop(Int, String)    //screen size, model, weight    case Phone(Int, String, Double)    //screen size, model, weight    case Tablet(Int, String, Double)}var tech:Computer = .Desktop(8, "i5")

Here you have made a computer that is of type Computer, with a value of Desktop, with 8 GB of RAM and an i5 processor. Notice how you can give each member value different required associated values. ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.