如何从 Swift 中的集合中获取随机元素?

2023-07-08移动开发问题
3

本文介绍了如何从 Swift 中的集合中获取随机元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

从 Swift 1.2 开始,Apple 引入了 Set 集合类型.

As of Swift 1.2, Apple introduces Set collection type.

说,我有一套像:

var set = Set<Int>(arrayLiteral: 1, 2, 3, 4, 5)

现在我想从中得到一个随机元素.问题是如何?Set 不像 Array 那样提供 subscript(Int).相反,它有 subscript(SetIndex<T>).但首先, SetIndex<T> 没有可访问的初始化程序(因此,我不能只创建具有所需偏移量的索引),其次,即使我可以获得第一个元素的索引一个集合 (var startIndex = set.startIndex) 那么我可以得到第 N 个索引的唯一方法是通过连续调用 successor().

Now I want to get a random element out of it. Question is how? Set does not provide subscript(Int) like Array does. Instead it has subscript(SetIndex<T>). But firstly, SetIndex<T> does not have accessible initializers (hence, I can not just create an index with the offset I need), and secondly even if I can get the index for a first element in a set (var startIndex = set.startIndex) then the only way I can get to the N-th index is through consecutive calls to successor().

因此,我目前只能看到 2 个选项,既丑又贵:

Therefore, I can see only 2 options at the moment, both ugly and expensive:

  • 将集合转换为数组(var array = [Int](set))并使用它的下标(完全接受Int);或
  • 获取集合中第一个元素的索引,遍历successor()方法链得到第N个索引,然后通过集合的下标读取对应的元素.
  • Convert the set into array (var array = [Int](set)) and use its subscript (which perfectly accepts Int); or
  • Get index of a first element in a set, traverse the chain of successor() methods to get to the N-th index, and then read corresponding element via set's subscript.

我想念其他方式吗?

推荐答案

可能最好的方法是 advance 为你走 successor:

Probably the best approach is advance which walks successor for you:

func randomElementIndex<T>(s: Set<T>) -> T {
    let n = Int(arc4random_uniform(UInt32(s.count)))
    let i = advance(s.startIndex, n)
    return s[i]
}

(嘿;注意到您实际上已更新问题以包含此答案,然后再将其添加到我的答案中……嗯,这仍然是个好主意,我也学到了一些东西.:D)

( Heh; noticed you actually updated the question to include this answer before I added it to my answer... well, still a good idea and I learned something too. :D)

你也可以遍历集合而不是索引(这是我的第一个想法,但后来我想起了 advance).

You can also walk the set rather than the indices (this was my first thought, but then I remembered advance).

func randomElement<T>(s: Set<T>) -> T {
    let n = Int(arc4random_uniform(UInt32(s.count)))
    for (i, e) in enumerate(s) {
        if i == n { return e }
    }
    fatalError("The above loop must succeed")
}

这篇关于如何从 Swift 中的集合中获取随机元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

CCScrollView 滚动和触摸事件永远不会触发
CCScrollView scroll and touch events never firing(CCScrollView 滚动和触摸事件永远不会触发)...
2024-08-12 移动开发问题
1

使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘
removing jagged edges of my ropes using antialiasing of OpenGLES(使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘)...
2024-08-12 移动开发问题
34

cocos2d 在场景之间移动
cocos2d Moving between scene(cocos2d 在场景之间移动)...
2024-08-12 移动开发问题
2