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 Swift Enums and Structs Enums and their Methods Methods or Member Functions

Lukes Ivi
Lukes Ivi
2,001 Points

.Self Confused

Hey guys,

I'm having a hard time understanding the self property. What does it access? What is it used for? When can it be used?

Thanks in advance for clearing things up...

2 Answers

Hi,

self is important to understand with programming in general, especially in languages where everything is an object.

To manipulate or add on to a particular instance of a object, self allows you to do so.

In most languages, iOS not being an exception, you create instance methods, or methods that work on instances of an object or an object that has fulfilled the appropriate criteria to be considered a valid object of a particular interface.

This is especially important for classes and structs.

To make a distinction on methods that apply to no specific instance of the class but to be used only by the class itself instead of an instance of a class, you are given self explicitly to properties or the state of things that originate from a class or struct (an instance of a class or struct).

In the course, If you have an instance of a Todo struct and want to change specific properties of a particular instance through methods in a don't-repeat-yourself fashion, you can create the methods applicable to an instance of a Todo struct and explicitly specific what specific properties you want to change within the methods using self.

Lukes Ivi
Lukes Ivi
2,001 Points

Thanks Kevin.