Skip to content

Commit a45e8c3

Browse files
committed
Nest the example delegate protocol.
1 parent 63d62a6 commit a45e8c3

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

TSPL.docc/LanguageGuide/Protocols.md

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,12 @@ The example below defines two protocols for use with dice-based board games:
829829
protocol DiceGame {
830830
var dice: Dice { get }
831831
func play()
832-
}
833-
protocol DiceGameDelegate: AnyObject {
834-
func gameDidStart(_ game: DiceGame)
835-
func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
836-
func gameDidEnd(_ game: DiceGame)
832+
833+
protocol Delegate: AnyObject {
834+
func gameDidStart(_ game: DiceGame)
835+
func game(_ game: DiceGame, didStartNewTurnWithDiceRoll diceRoll: Int)
836+
func gameDidEnd(_ game: DiceGame)
837+
}
837838
}
838839
```
839840

@@ -856,8 +857,15 @@ protocol DiceGameDelegate: AnyObject {
856857
The `DiceGame` protocol is a protocol that can be adopted
857858
by any game that involves dice.
858859

859-
The `DiceGameDelegate` protocol can be adopted
860+
The `DiceGame.Delegate` protocol can be adopted
860861
to track the progress of a `DiceGame`.
862+
Because the `DiceGame.Delegate` protocol
863+
is always used in the context of a dice game,
864+
it's nested inside of the `DiceGame` protocol.
865+
Protocols can be nested inside of other protocols
866+
and inside of type declarations like structures and classes,
867+
as long as the outer declaration isn't generic.
868+
861869
To prevent strong reference cycles,
862870
delegates are declared as weak references.
863871
For information about weak references,
@@ -872,7 +880,7 @@ as discussed in <doc:Protocols#Class-Only-Protocols>.
872880
Here's a version of the *Snakes and Ladders* game originally introduced in <doc:ControlFlow>.
873881
This version is adapted to use a `Dice` instance for its dice-rolls;
874882
to adopt the `DiceGame` protocol;
875-
and to notify a `DiceGameDelegate` about its progress:
883+
and to notify a `DiceGame.Delegate` about its progress:
876884

877885
```swift
878886
class SnakesAndLadders: DiceGame {
@@ -885,7 +893,7 @@ class SnakesAndLadders: DiceGame {
885893
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
886894
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
887895
}
888-
weak var delegate: DiceGameDelegate?
896+
weak var delegate: DiceGame.Delegate?
889897
func play() {
890898
square = 0
891899
delegate?.gameDidStart(self)
@@ -921,7 +929,7 @@ class SnakesAndLadders: DiceGame {
921929
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
922930
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
923931
}
924-
weak var delegate: DiceGameDelegate?
932+
weak var delegate: DiceGame.Delegate?
925933
func play() {
926934
square = 0
927935
delegate?.gameDidStart(self)
@@ -960,20 +968,20 @@ the class's `init()` initializer.
960968
All game logic is moved into the protocol's `play` method,
961969
which uses the protocol's required `dice` property to provide its dice roll values.
962970

963-
Note that the `delegate` property is defined as an *optional* `DiceGameDelegate`,
971+
Note that the `delegate` property is defined as an *optional* `DiceGame.Delegate`,
964972
because a delegate isn't required in order to play the game.
965973
Because it's of an optional type,
966974
the `delegate` property is automatically set to an initial value of `nil`.
967975
Thereafter, the game instantiator has the option to set the property to a suitable delegate.
968-
Because the `DiceGameDelegate` protocol is class-only, you can declare the
976+
Because the `DiceGame.Delegate` protocol is class-only, you can declare the
969977
delegate to be `weak` to prevent reference cycles.
970978

971-
`DiceGameDelegate` provides three methods for tracking the progress of a game.
979+
`DiceGame.Delegate` provides three methods for tracking the progress of a game.
972980
These three methods have been incorporated into the game logic within
973981
the `play()` method above, and are called when
974982
a new game starts, a new turn begins, or the game ends.
975983

976-
Because the `delegate` property is an *optional* `DiceGameDelegate`,
984+
Because the `delegate` property is an *optional* `DiceGame.Delegate`,
977985
the `play()` method uses optional chaining each time it calls a method on the delegate.
978986
If the `delegate` property is nil,
979987
these delegate calls fail gracefully and without error.
@@ -986,10 +994,10 @@ and are passed the `SnakesAndLadders` instance as a parameter.
986994
-->
987995

988996
This next example shows a class called `DiceGameTracker`,
989-
which adopts the `DiceGameDelegate` protocol:
997+
which adopts the `DiceGame.Delegate` protocol:
990998

991999
```swift
992-
class DiceGameTracker: DiceGameDelegate {
1000+
class DiceGameTracker: DiceGame.Delegate {
9931001
var numberOfTurns = 0
9941002
func gameDidStart(_ game: DiceGame) {
9951003
numberOfTurns = 0
@@ -1012,7 +1020,7 @@ class DiceGameTracker: DiceGameDelegate {
10121020
- test: `protocols`
10131021
10141022
```swifttest
1015-
-> class DiceGameTracker: DiceGameDelegate {
1023+
-> class DiceGameTracker: DiceGame.Delegate {
10161024
var numberOfTurns = 0
10171025
func gameDidStart(_ game: DiceGame) {
10181026
numberOfTurns = 0
@@ -1032,7 +1040,7 @@ class DiceGameTracker: DiceGameDelegate {
10321040
```
10331041
-->
10341042

1035-
`DiceGameTracker` implements all three methods required by `DiceGameDelegate`.
1043+
`DiceGameTracker` implements all three methods required by `DiceGame.Delegate`.
10361044
It uses these methods to keep track of the number of turns a game has taken.
10371045
It resets a `numberOfTurns` property to zero when the game starts,
10381046
increments it each time a new turn begins,

0 commit comments

Comments
 (0)