@@ -4,10 +4,9 @@ In [Getting Started](/getting-started/cli-swiftpm/),
44a simple command-line tool is built with the Swift Package Manager.
55
66To provide a more complete look at what the Swift Package Manager can do,
7- the following example consists of four interdependent packages:
7+ the following example consists of three interdependent packages:
88
99* [ PlayingCard] [ PlayingCard ] - Defines ` PlayingCard ` , ` Suit ` , and ` Rank ` types.
10- * [ FisherYates] [ FisherYates ] - Defines an extension that implements the ` shuffle() ` and ` shuffleInPlace() ` methods.
1110* [ DeckOfPlayingCards] [ DeckOfPlayingCards ] - Defines a ` Deck ` type that shuffles and deals an array of ` PlayingCard ` values.
1211* [ Dealer] [ Dealer ] - Defines an executable that creates a ` DeckOfPlayingCards ` , shuffles it, and deals the first 10 cards.
1312
@@ -75,64 +74,13 @@ it will compile the Swift module for `PlayingCard`.
7574> The complete code for the `PlayingCard` package can be found at
7675> <https://github.com/apple/example-package-playingcard>.
7776
78- ### Using Build Configuration Statements
79-
80- The next module you' re going to build is ` FisherYates` .
81- Unlike ` PlayingCard` , this module does not define any new types.
82- Instead, it extends an existing type --
83- specifically the ` Collection` and ` MutableCollection` protocols --
84- to add the `shuffled ()` method
85- and its mutating counterpart ` shuffle()` .
86-
87- The implementation of ` shuffle()` uses
88- the [Fisher-Yates](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle) algorithm
89- to randomly permute the elements in a collection.
90- Instead of using the random number generator provided by the Swift standard library,
91- this method calls a function imported from a system module.
92- For this function to be compatible with both macOS and Linux,
93- the code uses build configuration statements.
94-
95- In macOS, the system module is ` Darwin` ,
96- which provides the ` arc4random_uniform(_:)` function.
97- In Linux, the system module is ` Glibc` ,
98- which provides the ` random()` function:
99-
100- ~ ~~ swift
101- # if os(Linux)
102- import Glibc
103- # else
104- import Darwin.C
105- # endif
106-
107- public extension MutableCollection where Index == Int {
108- mutating func shuffle () {
109- if count < = 1 { return }
110-
111- for i in 0..< count - 1 {
112- # if os(Linux)
113- let j = Int(random () % (count - i)) + i
114- # else
115- let j = Int(arc4random_uniform(UInt32(count - i))) + i
116- # endif
117- swapAt(i, j)
118- }
119- }
120- }
121- ~ ~~
122-
123- > The complete code for the ` FisherYates` package can be found at
124- > < https://github.com/apple/example-package-fisheryates> .
125-
12677### Importing Dependencies
12778
128- The ` DeckOfPlayingCards` package brings the previous two packages together:
129- It defines a ` Deck` type
130- that uses the `shuffle ()` method from ` FisherYates`
131- on an array of ` PlayingCard` values.
79+ The `DeckOfPlayingCards` package brings in the previous package:
80+ It defines a `Deck` type.
13281
133- To use the ` FisherYates` and ` PlayingCards` modules,
134- the ` DeckOfPlayingCards` package must declare their packages as dependencies
135- in its ` Package.swift` manifest file.
82+ To use the `PlayingCards` module, the `DeckOfPlayingCards` package must declare
83+ the package as a dependency in its `Package.swift` manifest file.
13684
13785~~~ swift
13886// swift-tools-version:5.3
@@ -144,13 +92,12 @@ let package = Package(
14492 .library(name: "DeckOfPlayingCards", targets: ["DeckOfPlayingCards"]),
14593 ],
14694 dependencies: [
147- .package(url: " https://github.com/apple/example-package-fisheryates.git" , from: " 2.0.0" ),
14895 .package(url: "https://github.com/apple/example-package-playingcard.git", from: "3.0.0"),
14996 ],
15097 targets: [
15198 .target(
15299 name: "DeckOfPlayingCards",
153- dependencies: [" FisherYates " , " PlayingCard" ]),
100+ dependencies: ["PlayingCard"]),
154101 .testTarget(
155102 name: "DeckOfPlayingCardsTests",
156103 dependencies: ["DeckOfPlayingCards"]),
@@ -163,9 +110,7 @@ The source URL is a URL accessible to the current user that resolves to a Git re
163110The version requirements,
164111which follow [Semantic Versioning (SemVer)](http://semver.org) conventions,
165112are used to determine which Git tag to check out and use to build the dependency.
166- For the ` FisherYates` dependency,
167- the most recent version with a major version equal to ` 2` (for example, ` 2.0.4` ) will be used.
168- Similarly, the ` PlayingCard` dependency will use the most recent version with a major version equal to ` 3` .
113+ For the `PlayingCard` dependency will use the most recent version with a major version equal to `3`.
169114
170115When the `swift build` command is run,
171116the Package Manager downloads all of the dependencies,
@@ -186,7 +131,7 @@ and intermediate build products in the `.build` directory at the root of your pr
186131With everything else in place,
187132now you can build the `Dealer` module.
188133The `Dealer` module depends on the `DeckOfPlayingCards` package,
189- which in turn depends on the ` PlayingCard` and ` FisherYates ` packages .
134+ which in turn depends on the `PlayingCard` package .
190135However, because the Swift Package Manager automatically resolves transitive dependencies,
191136you only need to declare the `DeckOfPlayingCards` package as a dependency.
192137
@@ -216,9 +161,6 @@ that are referenced in code.
216161For the `Dealer` module' s ` main.swift` file,
217162the ` Deck` type from ` DeckOfPlayingCards`
218163and the ` PlayingCard` type from ` PlayingCard` are referenced.
219- Although the `shuffle()` method on the `Deck` type
220- uses the `FisherYates` module internally,
221- that module does not need to be imported in `main.swift`.
222164
223165~ ~~ swift
224166import DeckOfPlayingCards
@@ -271,6 +213,5 @@ see the documentation provided in the [Swift Package Manager project on GitHub](
271213
272214
273215[PlayingCard]: https://github.com/apple/example-package-playingcard
274- [FisherYates]: https://github.com/apple/example-package-fisheryates
275216[DeckOfPlayingCards]: https://github.com/apple/example-package-deckofplayingcards
276217[Dealer]: https://github.com/apple/example-package-dealer
0 commit comments