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

Why not self.strength?

In this video, the override method is using the decreaseHealth method, as the following

enemy.decreaseHealth(strength)

Why not self.strength, given the fact that we are calling the self property of the subclass?

The class sends its own strength parameter: 50 or 100. We use self when we want to call or work with instance. Here is we are not working with instance. We r just overriding method.

1 Answer

In most cases in Swift, the use of self is implied, and you don't always need to type self. before you access symbols from other methods on the same object. The only exception to this is inside of closures, in which self is captured incase the closure's lifetime is longer than self. That may sound complicated, but all you really need to know from that is that you don't have to write self, except inside of closures