개발/swift

Swift mutating function

lemonade 2020. 11. 1. 10:00
반응형

What is mutating function?

 

If you ever used structure or class, you might heard of mutating keyword.

You can use the mutating keyword in the instance method to modify the properties.


If you have tried using the mutating keyword in class methods,


the compiler will saying there is something wrong.

 

 

2020/10/01 - [개발/ios] - Swift structure vs class

 

Swift structure vs class

Comparing structure and class in swift In common, both can Define properties to store values Define subscripts to provide access to their values using subscript syntax Define initializers to set up..

lemon-high.tistory.com

The properties of value types cannot be modified within its instance methods by default.. 

 

With mutating keyword, your method have the ability to mutate the values of the properties.


Simple example of stack implementation using mutating functions.

 

 

Thanks for reading!

 

Happy coding :)

 

 

반응형
그리드형