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

iOS

Peter Fuhrey
Peter Fuhrey
2,146 Points

Does self refer to the actual data type?

Just a quick question. So like in Amit's enum example where he creates an enum Day and then goes through the initializer and methods and everything. When he's using self like in the init for example is that referring to the actual data type, so in this case Day?

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Peter,

Just for clarity self isn't generic to say the enum Day, this keyword refers to the instance of Day which is important to remember as most of the code we will be writing will be reusable; resulting in a lot of instances, one example of where self has no access is a class func which is similar to a standard function with the minor difference that it's contained within a static context meaning we don't need to first create an new instance of the class but we don't have access to self either.

class MyClass {
    class func sayHello(name: String) {
        println("Hello \(name)")
    }
}

MyClass.sayHello("Chris")

In the above example you can see I'm calling the sayHello method using MyClass without creating a new instance of it first, as I said above this is a static method therefore self can't be used.

Hope that helps.