High Level Explanations please

OK so I’ve finished half a dozen courses and followed the bouncing ball to create apps in CWC and others outside. The thing that strikes me, and annoys me to be honest, is that everyone to a man is coding away and then all of a sudden they put some keyword in and assume everyone just knows that it was obviously missing and that they know what it means and what it does.

Dear lecturers / teachers / experts etc etc… it is not so.

Let me start this topic with this gem:

“Obviously we’ll need to add @Environment this or that and then a modifier here and there and then this bit here and that bit there. Yes that should do it.”

But what does any of out really mean?

I went looking for Swift @Environment and was directed as normal to the Apple developers site. Now, to a Swift beginner, this is like reading a diatribe on enzymes and being expected to understand.

Here’s what it says:

“Environment
A property wrapper that reads a value from a view’s environment.”

Could someone take this apart for me, word by word?

For instance, what is “Environment”. We all know that in general it’s the stuff all around us. But what does it mean here, in relation to my app, my OS, my computer? Is it the same as “framework”? Is it sort of the same as “scope”.

Reading further down the Apple article, it appears to be a thing of many parts and variations, none of which mean anything to me, a seeker of knowledge, which I attribute to never having seen anything that describes the environment, for want of a better word, in which we are writing our code.

And then there is the “modifier” .extension, which seems to indicate that one can ‘extend’ this environment, Extend the environment? how is this different from ‘putting something into’ the environment.

I have my theories about what this ‘environment’ is, but id love to hear what others think it is and if possible, a link to a formal definition that is written for the masses, not the experience developers.

I don’t know either, but a visit to MS Copilot where I asked this question, “what is the ‘environment’ referring to in SwiftUI?”

Gave me pages of information that explained it perfectly.

So long legacy web search engines. :slight_smile:

Thanks, but what is MS Copilot?

AH, I see, it’s an AI. The result returned looks disarmingly like the Apple Developer pages, but neater somehow. I’ll give it and maybe ChatGPT a bit more of a poke!

I tried this out : “why does a swift class have to go into the environment” and a very interesting answer one gets indeed.

I still don’t really see any difference between declaring a struct and having it accessible all over the place and defining a class and having to put it into the environment. Surely once any variable is declared, its available app wide, unless it’s a private var?

I understand your frustration about this. But maybe I can encourage you to keep going. I’m programming since 40 years, 30 of them in a very different ‘environment’ in comparison to iOS, iPhone, Swift and SwiftUI (if you wan to know: ABAP in SAP-Systems).
So Swift, and terms like functional programming, structs, protocols, modifiers, views etc. are also new to me and very different from what I am used to. If I struggle with this (which I do a lot) I do the same like you describe: Search the web, read documentation or check Apple videos in the developer app, …
Maybe that is easier for me than for you as I might have more experience in general but sometimes that experience can be a burden at the same time when I automatically compare structs with instances, @Models with entities, protocols with interfaces.
Programming will always be searching for help and documentation, experimenting, testing, being frustrated the one day and euphoric the other. Don’t expect to understand everything instantly. Copy sample code und work with it, you’ll understand it later.

Or just use an AI bot to write half the code for you. :wink: It’s not too bad for little snippets.

Does the following article provide the high-level explanation of @Environment that you are looking for?

What is @Environment in SwiftUI

If not, what is missing in that article?

Your article is quite OK but I’m finding that it, and many others, are a bit superficial and based purely on accessing the standard available properties. A simple statement such as “The property modifier .font is one of those environment properties, and you use it all the time” would probably serve just as well.

My main concern about your article is the table splashed at the top of the article telling me all about these random classes that go to make up “the environment”. That information is of no concern as I don’t have to know they exist, all I need to know are the roots such as .dismiss. Your article would be clearer if you just removed it. You could also spell out these "roots’ as they like to call them, are simply the list of vars that are published in that EnvironmentValues document that you lkinkl to, with the “.” stuck in front of them.

IWhy not strait you article with some of those things that we already use, probably without any understanding of our interaction with the “environment,” such as dismiss and font.

I found your examples about changing the colour scheme accurate and interesting but the presentation is too fussy for my liking.

And then you could get to the nitty-gritty of my problem with all of these articles, they
a) don’t really say what “the Environment” is in plain language. Something like, "The Environment is other program, operating systems , apps and the like that your app exists in. Many properties of that environment are available to your app by simply accessing with the @Environment(.whateverroot) var whatever statement

b) they only touch the surface by mentioning this properties as mentioned above. What about decribing what it means and what happens when you add something of your own to the environment, vis EnvironmentObject etc. Surely this falls well within the bounds of a discussion on the environment.

c) and lastly, there is no mention of being able to extend the environment using .extionsion to add something to or modify the environment.

But I thank you profusely for taking the time to have written your article and sharing your thoughts with me.

Cheers!

I didn’t write the article.