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 self do?

Hi, I'm basically just confused now on what the self thing does in Swift. When you write out self = .Hello or something like that in an initializer, what does the self really mean or do?

Could anyone please explain what it means when it says self.row = row and self.column = column? Which row is which and which column is which?

1 Answer

It is the same as 'this' in Java and 'self' in Objective-C, but with Swift, self is only require when you call a property or method from a closure or to differentiate property names inside your code.

View here for more info https://github.com/raywenderlich/swift-style-guide#use-of-self

Hi Andreas, thanks for the help. I'm still a little confused on this though: class BoardLocation { let row: Int, column: Int

init(row: Int, column: Int) {
    self.row = row
    self.column = column

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

In this example, why does it say self.row = row and self.column = column? I find it confusing because it's just restating the same variable name, and I don't know what self is doing to differentiate the two rows and columns. Any help would be greatly appreciated!