|
Hello i dont get it why we use self in initialize of a struct (https://www.hackingwithswift.com/quick-start/beginners/how-to-create-custom-initializers) What name is equal to what name? There is only one constant name. Could anyone describe me this in other way or give some example? I mean this part of code:
In that code I thought the
|
|
Filip is confused by a common convention:
You are not alone! This convention of
One way to reduce this confusion is to NOT give them the same name.
So when you need to create a new student in your application you'd do this:
This eliminates the need to code |
|
This code is defining a struct named
Now, imagine we create 3 separate players using the initializer with some code like this...
When we were defining the struct itself, it had no way of knowing what we would name our players, or how to identify them individually, because they didn't exist yet. So, we use So, when we are creating Joey, it knows to only set Joey's name to be Joey, without changing the name of any other player that has previously been created. It will only set the |
|
struct SuperHero { var nickname: String var powers: [String] init(nickname: String, superPowers: [String]) { self.nickname = nickname self.powers = superPowers } } let batman = SuperHero(nickname: "The Caped Crusader", superPowers: ["He's really rich"]) (this is one of your exercices/example) Hi. So, if I understand it well, in line 6, the "self." is not mandatory (for superPowers<>powers...)? Thank you. |
|
Correct. When In the structure there are two parameters named As |
|
You have this
so you could change it to this
see no |
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.