🐦swift Left side of mutating operator isn't mutable: 'self' is immutable

swift

fukurou

the supreme coder
ADMIN
Code:
struct Person {

// define two properties
 var name = ""
 var age = 0
    func birthday(){
        age += 1
    }
}

// create instance of Person
var person1 = Person()

birthday attempts to change the struct from within the struct
so you need to add the prefix mutating :

Swift:
struct Person {

struct Person {
    
    // define two properties
    var name = ""
    var age = 0
    mutating func birthday(){
        self.age += 1
    }
}

// create instance of Person
var person1 = Person()
person1.birthday()
print(person1.age)
 
Last edited:
Top