0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ObservableでdidSetを使うとおかしくなる時の対処法 今回はUserDefaults

Posted at

結論から言うと、@ObservationIgnored を使います。
ただし、これを使うと該当のプロパティが更新されてもViewは更新されなくなるので、Class内でしか使わないプロパティや、もしくはViewでの描画に使わないプロパティでしたらこのやり方でどうにかなるはずです。

正直こう言う仕様なのか、バグなのか私はわかりませんが、とりあえず下記のやり方で解決したので、このやり方で間に合う方はそれでいいでしょう。

おかしくなるコード

    var second_interval: Int = 1{
        didSet {
           UserDefaults.standard.set(second_interval, forKey: "MyKey")
        }
    }

修正後
@ObservationIgnoredをつけてください。

    @ObservationIgnored var second_interval: Int = 1{
        didSet {
            UserDefaults.standard.set(second_interval, forKey: "MyKey")
        }
    }
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?