Check Mobile App
Read now
Start a Project

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.


When I think in getting solutions for my Ecommerce Platform, I think of Webkul. They have been such a blessing in our business. We have spent thousands of dollars in support and modules from them. We love their support team. They work very closely with us, and they always go the extra mile when we need help.

Michael Urresta

Michael Urresta

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