Related to this question, but a different error: Other Question
I have tried both the Swift 3 as well as the Swift 4 code from this question:
I am using Swift 3.2, and when using the Swift 3 code it yelled at me for not using SwapAt, so I've included the Swift 4 code, but I tried both, with the same result.
When using the shuffle() method, I get the error Variable 'offerCardsShuffled' inferred to have type '()', which may be unexpected
, and the obvious error afterwards, Value of tuple type '()' has no member 'enumerated'
I'm not sure what would be causing this, as other people using this code don't seem to be having that problem. Why are my shuffled arrays coming up as empty tuples?
Note I am fairly new to Swift development so a thorough explanation is definitely helpful.
func displayOfferCards() -> Void {
//let offerCardsr = allOfferCards().reversed()
var offerCards = allOfferCards()
var offerCardsShuffled = offerCards.shuffle()
for (index, offerCard) in offerCardsShuffled.enumerated() {
let delay = Double(index) * 0.2
offerCard.display(delay: delay)
}
}
}
func allOfferCards() -> [OfferCard]{
guard dataSource != nil else {
return []
}
let numberOfCards = self.dataSource!.kolodaNumberOfCards(self)
var offerCards = [OfferCard]()
for i in 0..<numberOfCards {
let offerCard = viewForCard(at: i)
if let offerCard = offerCard {
offerCards.append(offerCard as! OfferCard)
}
}
return offerCards
}
extension MutableCollection {
/// Shuffles the contents of this collection.
mutating func shuffle() {
let c = count
guard c > 1 else { return }
for (firstUnshuffled, unshuffledCount) in zip(indices, stride(from: c, to: 1, by: -1)) {
let d: IndexDistance = numericCast(arc4random_uniform(numericCast(unshuffledCount)))
let i = index(firstUnshuffled, offsetBy: d)
swapAt(firstUnshuffled, i)
}
}
}
extension Sequence {
/// Returns an array with the contents of this sequence, shuffled.
func shuffled() -> [Element] {
var result = Array(self)
result.shuffle()
return result
}
}