Created
March 27, 2017 17:17
-
-
Save JohnSundell/5944877c8ec5c651c9791b364d171a1c to your computer and use it in GitHub Desktop.
Code sample #3 from https://medium.com/@johnsundell/simple-swift-dependency-injection-with-functions-7b0ec5b49e60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CardGame { | |
typealias Randomizer = (UInt32) -> UInt32 | |
private let deck: Deck | |
private let randomizer: Randomizer | |
init(deck: Deck, randomizer: @escaping Randomizer = arc4random_uniform) { | |
self.deck = deck | |
self.randomizer = randomizer | |
} | |
func drawRandomCard() -> Card { | |
let index = randomizer(deck.count) | |
let card = deck[index] | |
return card | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment