Combineの@PublishedはwillSet的挙動をする

p0dee
Apr 17, 2021

久々にCombineを触っていて、 @Publishedを用いたプロパティの監視を試みていたところ、以下のようにwillSet的な挙動をしていた。

ドキュメントにも以下のような記載があったので、そのような仕様らしい。

When the property changes, publishing occurs in the property’s willSet block, meaning subscribers receive the new value before it’s actually set on the property.

とはいえ `newValue` は取得できているので、以下のように関数をメンバー数でなくパラメタをもとに実装しておけば、常に最新の状態に基づく処理を実行できる。

--

--

No responses yet