LoginSignup
0
1

More than 1 year has passed since last update.

メソッド内のストアドプロパティの変更にはmutatingキーワードが必要

Posted at
struct SomeStruct {
    var id: Int

    init(id: Int) {
        self.id = id
    }

    mutating func someMethod() {
        id = 4
    }
}

var a = SomeStruct(id: 1)
a.someMethod()
a.id // 4

構造体のストアドプロパティの変更には再代入を必要とするため、ストアドプロパティの変更を含むメソッドにはmutatingキーワードが必要。

struct SomeStruct {
    var id: Int

    init(id: Int) {
        self.id = id
    }

   func someMethod() {
        id = 4 // mutiaingがついていないのでエラーになる
    }
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1