O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

For the More Curious: Mutating Methods

After working through this chapter, you might be wondering about the mutating keyword. Why is it needed to allow you to modify a struct or enum? A useful place to start is to revisit functions that return functions.

Create a new playground called Mutating. Add a simple function to your playground that returns a String greeting.

Listing 15.16 A simple greeting function

func greet(name: String, withGreeting greeting: String) -> String {
    return "\(greeting) \(name)"
}

The greet(name:withGreeting:) function takes two arguments: a name and a greeting. It constructs and returns a greeting based on these two arguments. This function is straightforward to use.

Listing 15.17 Using greet(name:withGreeting:) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required