Skip to content

Using the mutating keyword in Swift

Swift, Mutating, Programming2 min read

The Swift programming language offers various powerful features to make code more expressive and efficient. One such feature is the mutating keyword, which allows us to modify value types within methods and functions. In this article, we'll delve into the concept of mutating in Swift and explore some examples to understand its usage.

What is mutating?

In Swift, structures and enumerations are value types. By default, when you define a method or function within a value type, it cannot modify the instance's properties. However, there are scenarios where we may want to change the state of a value type. This is where the mutating keyword comes into play.

By marking a method or function as mutating, we indicate that it can modify the properties of the value type it belongs to. It enables us to change the internal state of a structure or enumeration instance.

Using mutating in Methods

Let's start with a simple example of a Point structure representing a coordinate on a 2D plane:

1struct Point {
2 var x: Double
3 var y: Double
5 mutating func moveBy(x deltaX: Double, y deltaY: Double) {
6 x += deltaX
7 y += deltaY
8 }

In the above code, we define a moveBy(x:y:) method that adjusts the x and y values of a Point instance by a given delta. The method is marked as mutating because it modifies the properties of the structure.

Here's how we can use the moveBy(x:y:) method:

1var point = Point(x: 0.0, y: 0.0)
2print("Initial point: (\(point.x), \(point.y))") // Output: Initial point: (0.0, 0.0)
4point.moveBy(x: 2.5, y: 4.5)
5print("Updated point: (\(point.x), \(point.y))") // Output: Updated point: (2.5, 4.5)

As you can see, the moveBy(x:y:) method modifies the x and y values of the point instance, allowing us to update its state.

Using mutating in Functions

In addition to methods, we can also use the mutating keyword in functions to modify value types. Let's consider an example where we have a custom Double extension that doubles the value of a given number:

1extension Double {
2 mutating func doubleValue() {
3 self *= 2.0
4 }

The doubleValue() function is marked as mutating since it modifies the value of the Double instance it is called on.

Here's how we can use the doubleValue() function:

1var number = 5.0
2print("Initial number: \(number)") // Output: Initial number: 5.0
5print("Doubled number: \(number)") // Output: Doubled number: 10.0

By invoking the doubleValue() function on the number variable, we double

its value, as indicated by the output.

Limitations of mutating

While the mutating keyword is powerful, it can only be used with value types like structures and enumerations. It cannot be applied to classes since classes are reference types, and their instances are shared and mutated through reference rather than direct modification.

Wrap Up

The mutating keyword in Swift allows us to modify value types within methods and functions. By marking a method or function as mutating, we gain the ability to change the internal state of a structure or enumeration. This feature adds flexibility and convenience when working with value types, enabling us to update their properties. However, it's essential to note that the mutating keyword cannot be used with classes.