Enumerations and methods

Hey team, I couldn’t find the correct category, so pardon me if it’s wrong place. Currently I’m learning Programming in Swift: Functions & Types and I’m stuck in Properties & Methods -> Methods part. So what is not clear for me is why the instructor used self there and overall explanation is not clear. I rewatched it several times but no result. Could you please try to elaborate on such new parts of the course so it could be smooth path for everyone?

Self thing overall is not new for me but it’s not clear why she used it there and why it was assigned to Weekday.allCases[indexOfAdvancedDay % Weekday.allCases.count]

Thanks beforehand!

@catie please see above

Hello! Thanks for your question. There are usually forum posts for each episode in a course, but they’re not easy to find at the moment :]

You can read more about the use of self in Swift in our Swift Apprentice book in the Methods chapter under “Introducing self”:

To try to summarize: self represents the value of an instance. When you use self in a method of a type, you are referring to the instance you are calling the method on. Two common cases to use self are:

1- to disambiguate between things with the same name (like when setting values on properties in initializers)
2 - or, as in the episode you refer to, to directly reassign the value of the entire instance. This is a common way to mutate enum instances in Swift!

Thanks for the explanation. I appreciate that :bowing_woman:

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.