菜鸟教程小白 发表于 2022-12-11 17:36:33

swift - 随机播放数组 swift 3


                                            <p><p>如何将下面的函数转换为 <code>swift 3</code>?当前出现 <code>二元运算符 '..<' 无法应用于 'Int' 和 'Self.IndexDistance' 类型的操作数</code> 错误。</p>

<pre><code>extension MutableCollection where Index == Int {
/// Shuffle the elements of `self` in-place.
mutating func shuffleInPlace() {
    // empty and single-element collections don&#39;t shuffle
    if count &lt; 2 { return }

    for i in 0..&lt;count - 1 { //error takes place here
      let j = Int(arc4random_uniform(UInt32(count - i))) + i
      guard i != j else { continue }
      swap(&amp;self, &amp;self)
    }
}
}
</code></pre>

<p>引用:<a href="https://stackoverflow.com/a/24029847/5222077" rel="noreferrer noopener nofollow">https://stackoverflow.com/a/24029847/5222077</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>count</code> 返回一个 <code>IndexDistance</code> 这是描述的类型
两个集合索引之间的距离。 <code>IndexDistance</code> 是
必须是 <code>SignedInteger</code>,但不必是 <code>Int</code> 并且可以
不同于 <code>Index</code>。因此无法创建
范围 <code>0..<count - 1</code>.</p>

<p>一种解决方案是使用 <code>startIndex</code> 和 <code>endIndex</code> 代替 <code>0</code> 和 <code>count</code>:</p>

<pre><code>extension MutableCollection where Index == Int {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffle() {
      // empty and single-element collections don&#39;t shuffle
      if count &lt; 2 { return }

      for i in startIndex ..&lt; endIndex - 1 {
            let j = Int(arc4random_uniform(UInt32(endIndex - i))) + i
            if i != j {
                swap(&amp;self, &amp;self)
            }
      }
    }
}
</code></pre>

<p>另一个优点是这也适用于数组 <em>slices</em>
(其中第一个元素的索引不一定为零)。</p>

<p>注意,根据新的 <a href="https://swift.org/documentation/api-design-guidelines/" rel="noreferrer noopener nofollow">&#34;Swift API Design Guidelines&#34;</a> ,
<code>shuffle()</code> 是变异 shuffle 方法的“正确”名称,
和 <code>shuffled()</code> 用于返回数组的非变异副本:</p>

<pre><code>extension Collection {
    /// Return a copy of `self` with its elements shuffled
    func shuffled() -&gt; {
      var list = Array(self)
      list.shuffle()
      return list
    }
}
</code></pre>

<p><em>更新:</em> 一个(更通用的)Swift 3 版本已添加到
同时<a href="https://stackoverflow.com/questions/24026510/how-do-i-shuffle-an-array-in-swift/24029847#24029847" rel="noreferrer noopener nofollow">How do I shuffle an array in Swift?</a>。</p>

<hr/>

<p>对于 <strong>Swift 4 (Xcode 9)</strong>,必须替换对 <code>swap()</code> 的调用
通过调用集合的 <code>swapAt()</code> 方法来实现。
也不再需要对 <code>Index</code> 类型的限制:</p>

<pre><code>extension MutableCollection {
    /// Shuffle the elements of `self` in-place.
    mutating func shuffle() {
      for i in indices.dropLast() {
            let diff = distance(from: i, to: endIndex)
            let j = index(i, offsetBy: numericCast(arc4random_uniform(numericCast(diff))))
            swapAt(i, j)
      }
    }
}
</code></pre>

<p>有关 <code>swapAt</code> 的更多信息,请参阅 <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0173-swap-indices.md" rel="noreferrer noopener nofollow">SE-0173 Add <code>MutableCollection.swapAt(_:_:)</code></a>。</p>

<hr/>

<p>从 <strong>Swift 4.2</strong>(Xcode 10,目前处于测试阶段)开始,实现了
<a href="https://github.com/apple/swift-evolution/blob/master/proposals/0202-random-unification.md" rel="noreferrer noopener nofollow">SE-0202 Random Unification</a> ,
<code>shuffle()</code> 和 <code>shuffled()</code> 是 Swift 标准库的一部分。</p></p>
                                   
                                                <p style="font-size: 20px;">关于swift - 随机播放数组 swift 3,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39615614/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39615614/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: swift - 随机播放数组 swift 3