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

Can anyone please walk me through understanding this syntax?

Forums > SwiftUI

extension EnvironmentValues {
    var myCustomValue: String {
        get { self[MyEnvironmentKey.self] }
        set { self[MyEnvironmentKey.self] = newValue }
    }
}

Specifically the bit:

self[MyEnvironmentKey.self] 

I just don't get what's going on here.

2      

extension EnvironmentValues {
    var myCustomValue: String {
        get { self[MyEnvironmentKey.self] }
        set { self[MyEnvironmentKey.self] = newValue }
    }
}

Tha above code is an Extension of EnvironmentValues, which must be a type , if you know in compiler design , we have types, they represent different data that can be adjusted in them and how we can manipulate them.

1) So it ptovies a way to extend the features of type EnvironmentValue

2) the code var myCustomValue: String { represents a coding pattern in which whenever myCustomValue will be accessed like say called to be displayed inside label.

3) the value that will be sent to caller site , where it is requested will be dicded by get and set will be allowed to change it or assign.

4) get { self[MyEnvironmentKey.self] } --> self(first one) simply represents an object of what we are dealing with and are inside of it, in this case EnvironmentValues.

5) then MyEnvironmentKey.self this code is there to tell the compiler to work with the type itself rather then any of its object, it is helpful in some cases where the compiler needs more information, really as a lot of code is hidden from us and implemented behind the scenes, a lot of such info we should know are now not known to us.

So yes the code is going to be like this as the compiler needs clear instruction, do not allow words to confuse you , remain focused on logic , its really simple for human mind not for compiler , hence all this code.

2      

I'll take a crack at it, though I'm not 100% sure I'm right.

So, EnvironmentValues is basically holding a Dictionary of values that are part of the "Environment". You can get an idea of what Dictionary values are included in the "Environment" dictionary by default on this page

But in this example, you are trying to add your own custom-made dictionary entry to be included in EnvironmentValues, even though it isn't there by default. So, you are defining what what will happen when you get and set the value of this new dictionary entry that you are creating.

when you get the value of it...

get { self[MyEnvironmentKey.self] }

The first self refers to the EnvironmentValues Dictionary, and the MyEnvironmentKey.self refers to the name of the key that you have created.

So, it is basically saying, "When I look in the EnvironmentValues dictionary for the key myEnvironmentKey, tell me what is stored as the value there."

As an example, if you created a Dictionary like this...

var animals: [String: String] = ["cat": "furry feline", "dog": "furry canine"]

you could get the value of the "cat" key by using this...

animals["cat"]

and it would return "furry feline"

but in your example, you are basically just using

EnvironmentValues[myEnvironmentKey]

instead of

animals["cat"]

2      

Just a small addition EnvironmentValues is a struct. And it contains properties colorScheme, locale and many more. To access those properties we are using keypaths. Basically, we creating custom properties and accessing or changing values of those properties using keypaths.

2      

The first self refers to the EnvironmentValues Dictionary, and the MyEnvironmentKey.self refers to the name of the key that you have created.

To clarify just a bit, the EnvironmentValues dictionary uses as its key not a String or Int or anything else you might be used to seeing in a dictionary, but rather it uses a type that conforms to the EnvironmentKey protocol:

subscript<K>(key: K.Type) -> K.Value where K : EnvironmentKey

So, since you need to refer to a type when accessing those dictionary values, you use MyEnvironmentKey.self, which is the Swift way to make a reference to a type itself.

2      

Certainly! To understand a specific syntax, it would be helpful to provide the code snippet or the specific syntax you would like to learn about. Once you provide the code or syntax, I'll be happy to walk you through its meaning and usage.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.