LearnWhat are Enums and Structs in Swift?

Enums and Structs
   
Avatar

Amit Bijlani
writes on October 28, 2014

If you know C or Objective-C then you might be familiar with Enums and Structs. However, if you don’t know either of those languages then you are probably wondering what is their purpose and why you should use them. In Swift, both an Enum and Struct have an extended feature set from their predecessors keeping with their respective use cases.

Enum

enum or enumeration is a data type that allows you to define a list of possible values. If you are a web developer then the easiest way to think of an enum is anywhere you would use a dropdown. A dropdown consists of a limited set of values from which you can choose one. An enum allows you to create a data type with those set of values so that they can be recognized consistently throughout your app. For example, you are building a contacts application and wanted to categorize your contacts by: Friend, Family, Coworker and Other.

enum Type {
    case Friend
    case Family
    case Coworker
    case Other
}

Above is the simplest form of an enum. Note that each member value or member is separated by a case statement.

To create an instance of the enum:

var type = Type.Friend

Enums in Swift have a few more features like you can add an initializer method or custom methods to extend the functionality. If you didn’t want to select a specific member value when creating an instance then you can provide an init method which defaults to one of the member values.

enum Type {
    case Friend
    case Family
    case Coworker
    case Other

    init() {
        self = .Friend
    }
}

We will assume that every new contact we create is a friend. The init method is a special method that allows you to provide an initial value. Now that we have an init method you have to call it when creating a new instance.

var type = Type()

Struct

A struct allows you to create a structured data type which provides storage of data using properties and extending its functionality via methods.

Keeping with the theme of the contacts application. Let’s say you wanted to create a data type to represent a contact. This contact would contain three attributes: First Name, Last Name and Type. The type attributed would be of the enum we created previously. The attributes of a struct are known as properties or more specifically stored properties because they store values.

This is what the basic syntax of a contact struct would look like:

struct Contact {
    var firstName: String
    var lastName: String
    var type: Type
}

When we create an instance of a struct by default the compiler provides us with an initializer containing parameters for each of the three stored properties.

var contact = Contact(firstName: "Wade", lastName: "Watts", type: Type.Other)

Similar to an enum a struct can have an init method and custom methods to extend its functionality. If we wanted to use the default value for the type property then we would have to create our own custom initializer.

Value Types

It is important to know the difference between value and reference types when talking about Enums, Structs and Classes. Whenever you create an enum or a struct it is a value type, which means that its value is copied when assigned or passed.

var contact1 = Contact(firstName: "Wade", lastName: "Watts", type: Type.Other)

var contact2 = contact1

contact2.firstName = "Alan"

In the above example when you change the firstName property of the instance named contact2 it only changes it for contact2 and contact1 remains unchanged. That’s because a value type has only one owner. One of the major benefits of value types is that they are thread-safe not requiring any synchronization.

Learn More

There’s a lot more to Enums and Structs and the Swift Programming language book is a great resource for it. However, if you prefer video instructions then check out my new course which will be out in November: Swift Enums and Structs.

GET STARTED NOW

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Get Started

6 Responses to “What are Enums and Structs in Swift?”

  1. Saurabh on July 29, 2016 at 6:56 pm said:

    enum cases are not values. They are named constants which can be used in statements containing other types by using Int(), String(), Double(), Float ()

    cases can be provided rawValues where you have to mention the type of rawValue

  2. Nice article. Please fix the following typo: “Similar to an enum a struct can have an init method and custom methods to extend it’s functionality.”

    It should be “…its functionality.”

  3. Great Tutorial .It provide good knowledge to beginners of swift.

Leave a Reply

You must be logged in to post a comment.

man working on his laptop

Are you ready to start learning?

Learning with Treehouse for only 30 minutes a day can teach you the skills needed to land the job that you've been dreaming about.

Start a Free Trial
woman working on her laptop