如果我有以下枚舉:是否可以用Swift把一個元組放在一個枚舉中?
enum FruitTuple
{
static let Apple = (shape:"round",colour:"red")
static let Orange = (shape:"round",colour:"orange")
static let Banana = (shape:"long",colour:"yellow")
}
然後,我有以下功能:
static func PrintFruit(fruit:FruitTuple)
{
let shape:String = fruit.shape
let colour:String = fruit.colour
print("Fruit is \(shape) and \(colour)")
}
在fruit.shape
和fruit.colour
我得到的錯誤:
Value of type 'FruitTuple' has no member 'shape'
博覽會足夠,所以我改變枚舉有一個類型:
enum FruitTuple:(shape:String, colour:String)
{
static let Apple = (shape:"round",colour:"red")
static let Orange = (shape:"round",colour:"orange")
static let Banana = (shape:"long",colour:"yellow")
}
但隨後在枚舉聲明我得到的錯誤:
Inheritance from non-named type '(shape: String, colour: String)'
所以,問題是:它甚至有可能在一個枚舉一個數組和能夠引用它的組件部分以這種方式?我在這裏錯過了一些基本的東西嗎?
'FruitTuple.Apple'等只是元組,它們是* not *枚舉值(你沒有定義任何'case's)。 'enum FruitTuple'只提供一個「命名空間」(如https://stackoverflow.com/questions/38585344/swift-constants-struct-or-enum) –
相關:https://stackoverflow.com/questions/26387275/枚舉的元組功能於迅速。 –
乾杯,相關的問題有幫助。關於命名空間的觀點現在完全有意義了! – Wex