Grasp mutating function in Swift under three minutes!

Uplift iOS Interview

The Guide is for YOU if
  • You are preparing for an iOS interview and want to improve your skills and knowledge and looking to level up your interview game and land your dream job.
  • You want to gain confidence and ease during iOS interviews by learning expert tips and curated strategies.
  • You want access to a comprehensive list of iOS interview QA to practice and prepare.

The understanding of the mutating function depends on the understanding of reference and value types. In Swift, The properties of value types cannot be changed within its instance methods by default. You need to use a mutating keyword in the instance method to change the properties of a value type. The mutating function has the power to mutate the values of the properties.

The reference type (for example class) will share a single instance of the object and pass the same reference to a function/object. The value type (for example struct) will make a copy of it and passes only the value. You can also read Value and reference types in Swift -A deep dive for better understanding.

Why does a struct need mutation?

You need to use the mutating function if you will change any state contained within the struct. Calling a mutating function returns a new struct in place as Struct is immutable. It works the same as passing an inout parameter to a function. The mutating keyword lets callers know that the method is going to make the value change.

struct Person {
    var firstName = "Jacob"
    var lastName = "Ralf"

    // Here, we don't need mutating function because
    // it does not change the variable of the struct
    func fullName() -> String {
        return firstName + " " + lastName
    }

    // This function actually changes firstName and lastName
    // of the Person struct. Here, we need to use mutating
    // keyword because it is changing the variables in struct.
    mutating func capitalizeName() {
        firstName = firstName.uppercased()
        lastName = lastName.uppercased()
    }
}

// let person = Person()
// person.capitalizeName()
// Error: Cannot use mutating member on immutable 
// value: 'person' is a 'let' constant

var person = Person()
person.capitalizeName()
print(person.fullName())

// Output
// JACOB RALF

Calling a mutating functions on constants is not possible(e.g. let person = Person()). Because that would be the same thing as trying to assign the constant to a new value. Mutating functions can only be performed on variables.

Get ahead of the competition and ace your next iOS interview with expert-curated resources. Check out the collection of SwiftUI and Swift interview questions and answers, behavioral interview tips, and the popular custom layout contents. Plus, go through the new section for Engineering Manager to help you excel in your career!

Join my free Newsletter 🚀



✍️ Written by Ishtiak Ahmed

👉 Follow me on XLinkedIn



Get Ready to Shine: Mastering the iOS Interview




Enjoying the articles? Get the inside scoop by subscribing to my newsletter.

Get access to exclusive iOS development tips, tricks, and insights when you subscribe to my newsletter. You'll also receive links to new articles, app development ideas, and an interview preparation mini book.

If you know someone who would benefit from reading this article, please share it with them.