Mutating Function Use In Swift Structure To Change The Structure Value


The mutating keyword is only required if you are changing any state contained within the structure. In swift, classes are reference type whereas structures and enumerations are value types. Since Swift structs are immutable objects, calling a mutating function actually returns a new struct in-place (much like passing an inout parameter to a function). The mutating keyword lets callers know that the method is going to make the value change.

So usually you use the mutating keyword in front of a function in a structure or class. Purpose: Allow this function to change the value of the variables in this structure or class.

A need of the mutating Function In Struct

The problem is that when you create the struct Swift has no idea whether you will use it with constants or variables, so by default, it takes the safe approach: Swift won’t let you write methods that change properties unless you specifically request it.


now you are using the structure in our class.

Now we want to change the value of this structure object then we need to use the mutating function. because without this function you will get an error.

Now we will modify in Struct declaration section.

Now we can change the value of  the structure


So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.







. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.


It was a successful collaboration. The Mobikul team provided the client everything they needed in a timely and high-quality manner. They were very professional, reliable, and accessible during the entire process. The client also saw no room for any improvements in the vendor's performance.

Vincenzo Carrano

Vincenzo Carrano

Administrator, Segshop

Talk to Sales


Live Chat
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home