Containing information in enums

In Swift, there's a second value type that you can use in your apps. This type is called an enum. Enums are used to store a finite set of options, and they can be used as arguments to functions and methods, and also as properties on structs and classes. Enums are best used when you have property that's, for example, an Int or String, but it can't take all String or Int values. Alternatively, an enum can be used to describe a value for which there isn't a suitable type. In that case, the enum is the type.

Whenever you have a predetermined set of possible values, it's probably a wise idea for you to use an enum. Let's take a look at how you can define an enum in your own code:

enum MessageStatus { 
    case sent, delivered, read, unsent 
} 

This enum defines a set of statuses a message in a chat app could have. Storing this data in an enum allows us to use it as a status property on a message struct. Using an enum, we know that the status can never have an invalid value. Let's take a look at a quick example of using an enum as a property on a struct:

struct Message { 
    let contents: String 
    let status: MessageStatus 
} 
 
let message = Message(contents: "Hello, world", status: .unsent) 

Whenever you're referring to an enum value, you have two options. One is to write out the full enum type and the value you're referring to, for example, MessageStatus.unsent. Often, the Swift compiler will already know the type of enum you're accessing. This allows you to use a shorthand notation such as .unsent.

Enum values can do more than just provide a set of options that have no type. In Swift, an enum can have a type that describes the type of its values. For example, you could implement an enum that can have values that are of type String as follows:

enum FoodType: String { 
    case meat = "Meat" 
    case vegetables = "Vegetables" 
    case fruit = "Fruit" 
    case mixed = "Mixed food" 
} 

This FoodType enum specifies four types of food a pet might eat. Associated with the available options is a string value. This value is the enum's rawValue and can be accessed through the rawValue property.

An enum can't contain any stored properties, but it can contain computed properties. You can use these computed properties to associate even more information with the enum than you can through the rawValue. Let's take a look at an example:

enum FoodType: String { 
    case meat = "Meat" 
    case vegetables = "Vegetables" 
    case fruit = "Fruit" 
    case mixed = "Mixed food" 
     
    var description: String { 
        switch self { 
        case .meat: 
            return "A meaty food type" 
        case .vegetables: 
            return "Vegetables are good!" 
        case .fruit: 
            return "Fruity goodness" 
        case .mixed: 
            return "Just about anything edible" 
        } 
    } 
} 

The FoodType enum now has a description property that can be read. Let's take a look at an example of this as well:

let food: FoodType = .meat 
print(food.description) //A meaty food type 

As you can see, an enum behaves a lot like any other type, except you don't need an initializer to use it. You should also know that an enum case can actually act as a box for other values. A great example of this behavior is to look at a simplified version of the Optional datatype. Let's look at how an Optional containing an Int would look:

enum Optional { 
    case some(Int) 
    case none 
} 

This enum has two cases. The some case has a value associated with it between the braces. Creating and using an enum value such as this one is demonstrated in the following code:

let optionalInt: Optional = .some(10) 
if case let .some(value) = optionalInt { 
    print(value) // 10 
} 

To access the associated value, you can unwrap the enum value as shown in preceding section. To do so, we use Swift's powerful pattern matching in the form of a case let check. The last thing you need to know about enums is that they cannot inherit from other enums or classes. However, you can make enums conform to protocols, just like structs. This means that enums can't just have computed properties, they can contain instance and type functions as well. The main thing that sets structs apart from enums actually is the fact that enums are used to contain a finite set of possible cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.126.199