斯威夫特:选择了一个随机枚举值
我试图随机选择一个枚举值,这是我当前的尝试:斯威夫特:选择了一个随机枚举值
enum GeometryClassification {
case Circle
case Square
case Triangle
case GeometryClassificationMax
}
和随机选择:
let shapeGeometry = (arc4random() % GeometryClassification.GeometryClassificationMax) as GeometryClassification
这不过失败草草收场。
我得到这样的错误:
'GeometryClassification' is not convertible to 'UInt32'
如何解决,任何想法?
我不是疯了你最后的情况下,有 - 好像你包括.GeometryClassificationMax
只是为了使随机选择。在使用switch
声明的任何地方,您都需要考虑额外的情况,并且它没有语义值。相反,在enum
上的静态方法可以确定最大值并返回一个随机的情况,并且会更容易理解和维护。
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
private static let _count: GeometryClassification.RawValue = {
// find the maximum enum value
var maxValue: UInt32 = 0
while let _ = GeometryClassification(rawValue: maxValue) {
maxValue += 1
}
return maxValue
}()
static func randomGeometry() -> GeometryClassification {
// pick and return a new value
let rand = arc4random_uniform(_count)
return GeometryClassification(rawValue: rand)!
}
}
,现在您可以在switch
声明排出enum
:
switch GeometryClassification.randomGeometry() {
case .Circle:
println("Circle")
case .Square:
println("Square")
case .Triangle:
println("Triangle")
}
您需要为您的枚举指定一个原始类型。如果使用的是整数类型,则枚举的情况下的值将是自动生成的从0开始:
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
case GeometryClassificationMax
}
“不像C和Objective-C,在创建时夫特枚举成员没有被分配一个默认的整数值“。 - 根据this页面。指定整数类型让它知道以通常的方式生成值。
然后你就可以产生这样的随机值:
let randomEnum: GeometryClassification = GeometryClassification.fromRaw(arc4random_uniform(GeometryClassification.GeometryClassificationMax.toRaw()))!
这是一个可怕的丑陋的电话,以及所有那些“fromRaw”和'toRaw的叫声是相当不雅,所以我真的建议产生随机UInt32的你想先,然后创建从价值GeometryClassification是在范围:
GeometryClassification.fromRaw(someRandomUInt32)
在这种情况下,您不需要指定特定值;他们将从'0'开始自动生成。你从docs中引用的部分只有在'enum'不具有基于整数的原始类型时才是。进一步在同一个文档中:“当整数用于原始值时,如果某些枚举成员没有指定值,它们会自动递增。” – 2014-10-08 15:56:25
同样的错误仍然令人遗憾:/ – 2014-10-08 15:56:32
@SebastianFlückiger您需要从arc4random返回的原始值创建'GeometryClassification':'let shapeGeometry = GeometryClassification(rawValue:(arc4random()%GeometryClassification.GeometryClassificationMax.rawValue)) ' – 2014-10-08 15:58:36
既然你是枚举类中,具有随机的()方法中引用的最高值明确将消除不必每次都数得过来:
enum GeometryClassification: UInt32 {
case Circle
case Square
case Triangle
static func random() -> GeometryClassification {
// Update as new enumerations are added
let maxValue = Triangle.rawValue
let rand = arc4random_uniform(maxValue+1)
return GeometryClassification(rawValue: rand)!
}
}
这不适用于Swift 1.2。它想要一个rawValue分配给枚举。反过来,这意味着它只适用于具有Int rawValue类型的枚举。 – BadmintonCat 2015-05-14 06:46:42
我在我的代码上有一个错字,正在返回错误的类型。 – stevex 2015-05-14 10:36:29
这里是我的雨燕1.2的见解:
enum GeometryClassification : Int {
case Circle = 0
case Square = 1
case Triangle = 2
static func random() -> GeometryClassification {
let min = MutationType.Circle.rawValue
let max = MutationType.Triangle.rawValue
let rand = Int.random(min: min, max: max) // Uses ExSwift!
return self(rawValue: rand)!
}
}
你是对的 - 我最终用这种方法使它更具可读性。感谢你的努力。 – 2014-10-08 16:53:59
'++ maxValue'将在Swift 3中被弃用。你将如何去修复你的代码? – Cesare 2016-06-02 18:32:09
@Cesare:你可以在'while循环内移动增量。 – 2016-06-02 19:13:10