UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Day-9 Self

Forums > 100 Days of SwiftUI

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.

Paul said self points to whatever instance of the struct is currently being used.I dont understand it. Because he said it poins to the instance but in the other paragraph he said self point to the struct not instant. Can anyone explain this? Is it points to the instance or struct?

3      

but in the other paragraph he said self point to the struct not instant

Which other paragraph are you referring to?

Below is the paragraph right after the one you quoted. It's the only other paragraph in that section. I don't see where he says self points to the struct rather than the instance.

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 – self.name refers to the property, whereas name refers to the parameter.

Now, what you might be thinking of—and I'm not sure what day Paul gets around to talking about this—is the Self that points to a type, like a struct or class.

Self (with an uppercased s) refers to a type. self (with a lowercased s) refers to a particular instance of the type.

Example:

struct Thing {
    let name: String

    init(name: String) {
        self.name = name  //1
    }

    func copyWithReversedName() -> Self {  //2
        Thing(name: String(name.reversed()))
    }
}

//1: Here, self "points to whatever instance of the struct is currently being used", just as in Paul's first paragraph you quoted.

//2: Here, Self "lets you conveniently refer to the current type without repeating or knowing that type’s name" (per the Swift language docs). It indicates that the function copyWithReversedName() returns a Thing struct without actually using the name "Thing". This would be convenient should you, for example, later rename this type to "Person"; you wouldn't have to go through the struct's code and change all the places you used Thing because Self would just refer to whatever the struct is.

Hope I didn't just confuse you even more. ;)

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.