LoginSignup
3
1

More than 5 years have passed since last update.

[Swift] イニシャライザ内では willSet/didSet は呼ばれない

Posted at

タイトルのままです。
少しハマりました。

class A {

  var prop: Int {
    willSet { print("willSet", prop) }
    didSet { print("didSet", prop) }
  }

  init() {
    self.prop = 0
  }

  convenience init(_ prop: Int) {
    self.init()

    // convenienceイニシャライザ内で上書きしてもwillSet/didSetは呼ばれない
    self.prop = prop 
  }
}

let a = A()  // willSet/didSetは呼ばれない
let aa = A(3)  // こちらも呼ばれない
3
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
3
1