Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Alexander Certosimo
1,774 Pointswhat is self doing here?
I was wondering is someone could help me understand exactly what self is doing here? thanks for your time
enum Day: Int {
case Monday = 1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
func daysTillWeekend()->Int {
return Day.Saturday.rawValue - self.rawValue
}
}
var today = Day.Monday
today.daysTillWeekend()
4 Answers
Sam Chaudry
25,519 PointsSelf is a keyword used in Swift to refer to an instance of a object. This can be a Class, Enums Structs etc all of which are data structures containing data properties (i.e. weatherConditions:String).
In this case you have an enum called Day of type Int. So when you want to determine the rawValue of this enum you would say self.rawValue. Which is saying give me the rawValue of Enum named Day, or programatically self.rawValue.
Hope this clarifies things
Sam Chaudry
25,519 PointsYeah spot on
Alexander Certosimo
1,774 PointsHi Sam,
Thank you. just to clarify a bit more since i have been really trying to grasp the concept of object oriented programming. in the above example, when we assign Day.Monday to the variable total, we are creating an object of type Day, correct? Which we can also say we are creating an instance?
Sam Chaudry
25,519 PointsYeah basically you create object and instantiate them when you are using them. A good for you might be this:
https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/
Alexander Certosimo
1,774 PointsAlexander Certosimo
1,774 Pointsthank you, sam
Would the Enum Day be the instance and its members be the objects?