You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Read the post: <ahref="https://www.jessesquires.com/blog/2021/03/26/a-better-approach-to-writing-a-userdefaults-property-wrapper/">A better approach to writing a UserDefaults Property Wrapper</a></p>
71
71
<h4id='why-the-name' class='heading'>Why the name?</h4>
72
72
73
-
<p>Foil, as in let me quickly and easily <strong>wrap</strong> and <strong>store</strong> this leftover food in some <strong>foil</strong> so I can eat it later. 🌯 😉</p>
73
+
<p>Foil, as in “let me quickly and easily <strong>wrap</strong> and <strong>store</strong> this leftover food in some <strong>foil</strong> so I can eat it later.” 🌯 😉</p>
<p>There are <ahref="https://www.jessesquires.com/blog/2021/08/08/different-ways-to-observe-properties-in-swift/">many ways to observe property changes</a>. The most common are by using Key-Value Observing or a Combine Publisher. Both require the object with the property to inherit from <code>NSObject</code> and the property must be declared as <code>@objc dynamic</code>.</p>
129
+
<p>There are <ahref="https://www.jessesquires.com/blog/2021/08/08/different-ways-to-observe-properties-in-swift/">many ways to observe property changes</a>. The most common are by using Key-Value Observing or a Combine Publisher. KVO observing requires the object with the property to inherit from <code>NSObject</code> and the property must be declared as <code>@objc dynamic</code>.</p>
<p><strong>Note:</strong> that <code>average</code> does not need the <code>@objc dynamic</code> annotation, <code>.receiveValue</code> will fire immediately with the current value of <code>average</code> and on every change after.</p>
<h4id='combine-alternative-with-kvo' class='heading'>Combine Alternative with KVO</h4>
155
+
156
+
<p><strong>Note:</strong> in this case, <code>userId</code> needs the <code>@objc dynamic</code> annotation and <code>AppSettings</code> needs to inherit from <code>NSObject</code>. Then <code>receiveValue</code> will fire only on changes to wrapped object’s value. It will not publish the initial value as in the example above.</p>
0 commit comments