When I was learning Swift, I get very confused about keywords in front of functions and variables (or constants) like "static", "lazy", "mutating", etc. Well, there is a great thing called the internet, so I searched up and found out not all the definition is very specific and easy to understand. So today, I am going to tell you what does the mutating keyword does in front of a function.
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
Usage: mutating func (function name) () { }
Example:
struct myStructure {
var myVariableInt: Int = 8
mutating func changeMyVariableInt(newValue: Int) {
self.myVariableInt = newValue
}
}
Notices: If you just use a normal function without any keyword in front of it, you will get an error, so the purpose of mutating is allowing the function to change a value in this struct or class. That's all about mutating function
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
Usage: mutating func (function name) () { }
Example:
struct myStructure {
var myVariableInt: Int = 8
mutating func changeMyVariableInt(newValue: Int) {
self.myVariableInt = newValue
}
}
Notices: If you just use a normal function without any keyword in front of it, you will get an error, so the purpose of mutating is allowing the function to change a value in this struct or class. That's all about mutating function