Mutating Keyword in Swift

Javid Museyibli
Jan 2 · 3 min read

You probably have come across the mutating keyword in Swift and wonder what is it all about? To define it we need to understand how structs are organized in Swift first.

A struct is a data type that organizes property and methods in a unit. It has almost the same interface as a class with a few subtle differences. For example, once you set up a struct’s properties, it automatically generates memberwise initializers while a classes will not. The other significant difference is that while structs are value types, classes are reference types.

What does this mean? This basically means that when a struct instance is passed around, each time a new copy is created, whereas a class instance would use to the same reference when passed around. Changing a value of a property of class instance will not change the reference the object points to. However, because structs are value types and they are immutable, the below code will not even compile showing the error: Cannot assign to property: ‘self’ is immutable

struct Driver {
var drivingLicenseNumber: String?
func assignLisence(with number: String) {
drivingLicenseNumber = number
}
}

If you are using Xcode as your development tool it will also suggest you to mark method mutating to make self mutable.

Xcode suggestion to mark method mutating

Once we mark the method mutating the error will disappear and the code will function as intended. We have already stated that structs are value types and immutable. What effect does the mutating function have on a struct? Well, it basically creates a new copy of the struct with modified value of the the property. The effect of the mutating keyword is as simple as this.

Mutating functions come a lot handier when used in composition of protocols. Because protocols can be implemented by both classes and structs and structs are value types, it might be worthy to consider adding mutating keyword to functions which need to change the value of self. While classes will “ignore” this keyword, it will be make the methods of the struct able to change the value of self.

If a method is defined as immutable in the protocol, but later adds mutating keyword, it is regarded as two different functions and as a result struct fails to conform to the protocol. Thus, it is in our best interest to consider adding the keyword in the definition of the function within protocol.

One last thing… Similar to mutating keyword, there is also nonmutating keyword in Swift. By default functions in Swift are nonmutating. The nonmutating keyword is not useful in this context, but they can be useful when creating setters for properties. I will try to cover usages nonmutating keyword in upcoming articles. Keep connected :)

The Startup

Get smarter at building your thing. Join The Startup’s +794K followers.

Sign up for Top 10 Stories

By The Startup

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Javid Museyibli

Written by

Young enthusiastic software developer, currently full-time at PASHA Bank building Android and iOS applications.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

Javid Museyibli

Written by

Young enthusiastic software developer, currently full-time at PASHA Bank building Android and iOS applications.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +794K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store