{"id":3053,"date":"2023-06-06T21:20:06","date_gmt":"2023-06-07T01:20:06","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3053"},"modified":"2023-06-08T09:23:58","modified_gmt":"2023-06-08T13:23:58","slug":"discover-observation-in-swiftui","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/discover-observation-in-swiftui\/","title":{"rendered":"Discover Observation in SwiftUI"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" data-attachment-id=\"3110\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/discover-observation-in-swiftui\/attachment\/%ef%a3%bfwwdc23-9\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?fit=1920%2C1080&amp;ssl=1\" data-orig-size=\"1920,1080\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\uf8ffWWDC23-9\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3110\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-9.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>Let\u2019s you define models in standard swift types<\/p>\n\n\n\n<p>What is Observation?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>New feature for tracking changes to properties<\/li>\n\n\n\n<li>Will automatically create the code for you to track and use any changes occurs via Macros.&nbsp;<\/li>\n\n\n\n<li>Using a computed property, when it is changed the UI updates<\/li>\n\n\n\n<li>Adds performance improvements since the UI only changes when it needs to<\/li>\n<\/ul>\n\n\n\n<p>SwiftUI Property Wrappers<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You will still need @State if the view needs it\u2019s own state &#8211; like a sheet presentation<\/li>\n\n\n\n<li>@Environment allows you to propagate globally &#8211; Observable types work well here.&nbsp;<\/li>\n\n\n\n<li>@Bindable &#8211; is a lightweight just use $ syntax to create bindings.&nbsp; You will need that for things like TextFields<\/li>\n<\/ul>\n\n\n\n<p>Advanced uses<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can use arrays, optionals or any type that contains @Observable &#8211; it will track based on the specific instance<\/li>\n\n\n\n<li>If a computed property does not have any stored properties, you need to tell observation that the property was used and when it changes, you do this via creating your own getter and setter look this up for more detail.<\/li>\n<\/ul>\n\n\n\n<p>ObservableObject &#8211; how to update your code<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Observable macro will simplify your code<\/li>\n\n\n\n<li>Take a look at your public class &lt;name&gt; { } remove the @observableobject and @published, then change your view, all @Observed can be changed, mostly just delete annotations will get you there<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s you define models in standard swift types What is Observation? SwiftUI Property Wrappers Advanced uses ObservableObject &#8211; how to update your code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"hide_page_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2,3],"tags":[696,107,680],"class_list":["post-3053","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-observable","tag-day-2","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-Nf","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3053","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3053"}],"version-history":[{"count":2,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3053\/revisions"}],"predecessor-version":[{"id":3111,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3053\/revisions\/3111"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}