Grasp mutating function in Swift under three minutes!

Uplift iOS Interview

"Uplift iOS Interview" is a comprehensive guide to help aspiring iOS developers soar to new heights in their careers. This book is an indispensable tool for anyone looking to crack the iOS interview and impress their future employers with their technical prowess. With in-depth coverage of Swift, AutoLayout, SwiftUI, Multithreading, Memory management so on and so forth, this book is a treasure trove of knowledge for anyone looking to uplift their iOS development career.

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()

// Output

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 🚀

Rev Up Your iOS Skills: Take a Dynamic Learning Journey
iOS Career Boost

iOS Career Boost is the ultimate learning journey to elevate your iOS development career through a dynamic blend of visual learning, handy cheat sheets, coding practice materials, and expertly curated tips and tricks

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. Your email address will only be used for the purpose of sending the newsletter and will not be shared with third parties or advertisers. Rest assured that we value your privacy and will not spam your inbox.

Connect with me on

Twitter and LinkedIn and don't hesitate to reach out with any questions about this post. Thank you for reading.

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