【问题标题】:Argument type 'Range<Int>' does not conform to expected type 'Sequence' Swift3参数类型 'Range<Int>' 不符合预期的类型 'Sequence' Swift3
【发布时间】:2016-10-03 05:15:08
【问题描述】:

您好,我在 Swift3 之后遇到错误。我该如何解决这个错误?这些方法提供不重复的随机数。

 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
}
}

谢谢

【问题讨论】:

    标签: ios arrays swift range swift3


    【解决方案1】:

    使用范围的lowerBoundupperBound属性为[Int]的数组创建序列。

    var r = [Int](range.lowerBound..<range.upperBound)
    

    【讨论】:

    • 欢迎朋友 :)
    • 遗憾的是它不适用于符合 SignedInteger 的泛型。为此,您还需要使用 toIntMax() 进行转换
    【解决方案2】:

    我建议你使用CountableRange&lt;Int&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多