+ 1

Self Method

//In the code block below. What are both self methods refering to? do both self methods refer to the Suit enum because it is inside the enum block? Please see link for expanded inquiry. enum Suit { case spades, hearts, diamonds, clubs var rank: Int { switch self { case .spades: return 4 case .hearts: return 3 case .diamonds: return 2 case .clubs: return 1 } } func beats(_ otherSuit: Suit) -> Bool { return self.rank > otherSuit.rank } } https://code.sololearn.com/c9KIG0ab38pI/#swift

9th Sep 2018, 8:12 AM
Ran9
Ran9 - avatar
1 Réponse
+ 5
You can refer to the second slide in this lesson: https://www.sololearn.com/learn/Swift/2358/?ref=app Since self refers to the current instance, it would refer to whatever corresponds to your call within the enum. In this case, self invokes one of the four possible values. A call to the rank function via self would return the rank based on the value of the current instance of the enum.
9th Sep 2018, 9:03 AM
Hatsy Rei
Hatsy Rei - avatar