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

What does the self keyword mean?

I can't understand the use of the self keyword can someone please explain it to me.

2 Answers

This was confusing for me as well when I first started.

Check out https://github.com/raywenderlich/swift-style-guide#use-of-self

http://stackoverflow.com/questions/26835013/what-is-self-used-for-in-swift

Both links provide a good overview of self.

class BoardLocation {

  let row: Int, column: Int

  init(row: Int, column: Int) {
    self.row = row  // this is pointing to the init row
    self.column = column // this is pointing to the init column 

    let closure = {
      println(self.row)
    }
  }
}

Use self when required to differentiate between property names and arguments in initializers, and when referencing properties in closure expressions (as required by the compiler):

thanks !