Search code examples
swiftmutating-function

Is it possible to write mutating function in swift class?


I am able to write mutating functions in structure but not in class.

struct Stack {
    public private(set) var items = [Int]() // Empty items array

    mutating func push(_ item: Int) {
        items.append(item)
    }

    mutating func pop() -> Int? {
        if !items.isEmpty {
           return items.removeLast()
        }
        return nil
    }
}

Solution

  • In swift, classes are reference type whereas structures and enumerations are value types. The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the mutating keyword in the instance method. With this keyword, your method can then have the ability to mutate the values of the properties and write it back to the original structure when the method implementation ends.