NEW: Master Swift design patterns with my latest book! >>

< Previous: Initializers   Next: Lazy properties >

Referring to the current instance

Inside methods you get a special constant called self, which points to whatever instance of the struct is currently being used. This self value is particularly useful when you create initializers that have the same parameter names as your property.

For example, if you create a Person struct with a name property, then tried to write an initializer that accepted a name parameter, self helps you distinguish between the property and the parameter – refers to the property, whereas name refers to the parameter.

Here’s that in code:

struct Person {
    var name: String

    init(name: String) {
        print("\(name) was born!") = name

Need to know Objective-C fast?

I wrote a book dedicated to teaching Objective-C to developers who already know Swift – it's the fastest way to get up to speed!

< Previous: Initializers   Next: Lazy properties >
Click here to visit the Hacking with Swift store >>