Youtube App- Lesson 3 - Struct Constants

When constructing the API_URL in the struct constants why is it written as (Constants.API_KEY) rather than just (API_KEY)? Xcode is happy when I just write it without calling Constants first.

Is there any difference? Does it make the code less likely to break or is it needed so that when we call the API_URL in a different file then it knows where to find the other variables?

Probably the easiest to explain why it is referred to in that manner is to look at it from the point of view of the struct being the parent and the properties within the struct as child objects.

So you refer to them as Parent.child - ie, Constants.API_KEY or Constants.PLAYLIST_ID or Constants.API_URL

Within the struct you could refer to it as just the API_KEY but remember that when that API_URL is used elsewhere in your code (that is, outside that struct) you will need to refer to it by Constants.API_URL and also PLAYLIST_ID and API_KEY must also be prefixed with Constants as well since they themselves are referring to the other items in the struct.

Does that make sense?

Hi Chris,

Yes this makes sense. Though it seem’s odd that Swift would not just pick up that API_KEY and PLAYLIST_ID where also inside the struct if you had already called Constants.API_URL and they where referred to in that variable.