Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument type 'Range<Int>' does not conform to expected type 'Sequence' Swift3

Hi i get error after Swift3. How can i fix this error? These methods provide non-repeated random numbers.

 func uniqueRandoms(_ count: Int, inRange range: Range<Int>, blacklist: [Int] = []) -> [Int] {
    var r = [Int](range)
        .filter{ !blacklist.contains($0) }
        .shuffle()

    return Array(r[0..<count])
}



extension Array {
func shuffle() -> Array<Element> {
    var newArray = self

    for i in 0..<newArray.count {
        let j = Int(arc4random_uniform(UInt32(newArray.count)))
        guard i != j else { continue }
        swap(&newArray[i], &newArray[j])
    }

    return newArray
}
}

Thanks

like image 711
Emre Esen Avatar asked Oct 03 '16 05:10

Emre Esen


2 Answers

Use the lowerBoundand upperBound property of range to create sequence for Array of [Int].

var r = [Int](range.lowerBound..<range.upperBound)
like image 156
Nirav D Avatar answered Sep 22 '22 09:09

Nirav D


I would suggest you use CountableRange<Int>.

like image 42
eli_slade Avatar answered Sep 22 '22 09:09

eli_slade