@@ -829,11 +829,12 @@ The example below defines two protocols for use with dice-based board games:
829
829
protocol DiceGame {
830
830
var dice: Dice { get }
831
831
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
+ }
837
838
}
838
839
```
839
840
@@ -856,8 +857,15 @@ protocol DiceGameDelegate: AnyObject {
856
857
The ` DiceGame ` protocol is a protocol that can be adopted
857
858
by any game that involves dice.
858
859
859
- The ` DiceGameDelegate ` protocol can be adopted
860
+ The ` DiceGame.Delegate ` protocol can be adopted
860
861
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
+
861
869
To prevent strong reference cycles,
862
870
delegates are declared as weak references.
863
871
For information about weak references,
@@ -872,7 +880,7 @@ as discussed in <doc:Protocols#Class-Only-Protocols>.
872
880
Here's a version of the * Snakes and Ladders* game originally introduced in < doc:ControlFlow > .
873
881
This version is adapted to use a ` Dice ` instance for its dice-rolls;
874
882
to adopt the ` DiceGame ` protocol;
875
- and to notify a ` DiceGameDelegate ` about its progress:
883
+ and to notify a ` DiceGame.Delegate ` about its progress:
876
884
877
885
``` swift
878
886
class SnakesAndLadders : DiceGame {
@@ -885,7 +893,7 @@ class SnakesAndLadders: DiceGame {
885
893
board[03 ] = + 08 ; board[06 ] = + 11 ; board[09 ] = + 09 ; board[10 ] = + 02
886
894
board[14 ] = -10 ; board[19 ] = -11 ; board[22 ] = -02 ; board[24 ] = -08
887
895
}
888
- weak var delegate: DiceGameDelegate ?
896
+ weak var delegate: DiceGame.Delegate ?
889
897
func play () {
890
898
square = 0
891
899
delegate? .gameDidStart (self )
@@ -921,7 +929,7 @@ class SnakesAndLadders: DiceGame {
921
929
board[03] = +08; board[06] = +11; board[09] = +09; board[10] = +02
922
930
board[14] = -10; board[19] = -11; board[22] = -02; board[24] = -08
923
931
}
924
- weak var delegate: DiceGameDelegate ?
932
+ weak var delegate: DiceGame.Delegate ?
925
933
func play() {
926
934
square = 0
927
935
delegate?.gameDidStart(self)
@@ -960,20 +968,20 @@ the class's `init()` initializer.
960
968
All game logic is moved into the protocol's ` play ` method,
961
969
which uses the protocol's required ` dice ` property to provide its dice roll values.
962
970
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 ` ,
964
972
because a delegate isn't required in order to play the game.
965
973
Because it's of an optional type,
966
974
the ` delegate ` property is automatically set to an initial value of ` nil ` .
967
975
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
969
977
delegate to be ` weak ` to prevent reference cycles.
970
978
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.
972
980
These three methods have been incorporated into the game logic within
973
981
the ` play() ` method above, and are called when
974
982
a new game starts, a new turn begins, or the game ends.
975
983
976
- Because the ` delegate ` property is an * optional* ` DiceGameDelegate ` ,
984
+ Because the ` delegate ` property is an * optional* ` DiceGame.Delegate ` ,
977
985
the ` play() ` method uses optional chaining each time it calls a method on the delegate.
978
986
If the ` delegate ` property is nil,
979
987
these delegate calls fail gracefully and without error.
@@ -986,10 +994,10 @@ and are passed the `SnakesAndLadders` instance as a parameter.
986
994
-->
987
995
988
996
This next example shows a class called ` DiceGameTracker ` ,
989
- which adopts the ` DiceGameDelegate ` protocol:
997
+ which adopts the ` DiceGame.Delegate ` protocol:
990
998
991
999
``` swift
992
- class DiceGameTracker : DiceGameDelegate {
1000
+ class DiceGameTracker : DiceGame . Delegate {
993
1001
var numberOfTurns = 0
994
1002
func gameDidStart (_ game : DiceGame) {
995
1003
numberOfTurns = 0
@@ -1012,7 +1020,7 @@ class DiceGameTracker: DiceGameDelegate {
1012
1020
- test: `protocols`
1013
1021
1014
1022
```swifttest
1015
- -> class DiceGameTracker: DiceGameDelegate {
1023
+ -> class DiceGameTracker: DiceGame.Delegate {
1016
1024
var numberOfTurns = 0
1017
1025
func gameDidStart(_ game: DiceGame) {
1018
1026
numberOfTurns = 0
@@ -1032,7 +1040,7 @@ class DiceGameTracker: DiceGameDelegate {
1032
1040
```
1033
1041
-->
1034
1042
1035
- ` DiceGameTracker ` implements all three methods required by ` DiceGameDelegate ` .
1043
+ ` DiceGameTracker ` implements all three methods required by ` DiceGame.Delegate ` .
1036
1044
It uses these methods to keep track of the number of turns a game has taken.
1037
1045
It resets a ` numberOfTurns ` property to zero when the game starts,
1038
1046
increments it each time a new turn begins,
0 commit comments