— Swift, Mutating, Programming — 2 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.
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.
mutating
in MethodsLet's start with a simple example of a Point
structure representing a coordinate on a 2D plane:
1struct Point {2 var x: Double3 var y: Double4
5 mutating func moveBy(x deltaX: Double, y deltaY: Double) {6 x += deltaX7 y += deltaY8 }9}
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)3
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.
mutating
in FunctionsIn 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.04 }5}
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.02print("Initial number: \(number)") // Output: Initial number: 5.03
4number.doubleValue()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.
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.
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.