UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Why use @ObservedObject over @EnvironmentObject

Forums > SwiftUI

@00jim  

I just learnt that @EnvironmentObject does what @ObservedObject can do, but easier, so why use it ?

I understand that @StateObject doesn't force the data to reload like @ObservedObject can, but unsure about @EnvironmentObject. If not, then why would a developer bother with the hassle of typing the extra code ?

Is that that resouce intensive to use @EnvironmentObject ?

   

Check out this post What’s the difference between @ObservedObject, @State, and @EnvironmentObject?.

Note: that this acticle was written September 3rd 2021 and now use @Environment and @Observable.

Sharing SwiftUI state with @Observable

   

@kwolk  

I saw the changes in iOS17, I appreciate the streamlining.

However, why use the term "environment" ? The term Statement isn't use, it's State, so why not Environ ?

   

Think it @Environment because an object going into the setting(environment) of OS and can be access anywhere in the app, as where @State is a state of object in a View

   

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.