Skip to content

Extremely basic question about Dependencies motivation #3

Answered by mbrandonw
kamcma asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @kamcma,

It's a good question. The biggest reason why we can't leverage Environment is, as you alluded to, it cannot be used outside a view. Just put this somewhere outside a view to see:

@Environment(\.colorScheme) var colorScheme;
print(colorScheme)

You will get a purple runtime warning letting you know this isn't right:

🟣 Accessing Environment<ColorScheme>'s value outside of being installed on a View. This will always read the default value and will not update.

But, even if that wasn't the case, there would be more reasons to not leverage Environment. All the tools Apple provides to update an environment value are baked directly into views. There is just the view modifier, e.g. en…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@mbrandonw
Comment options

@kamcma
Comment options

Answer selected by kamcma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants