Reputation: 4696
If I have the following enum:
enum FruitTuple
{
static let Apple = (shape:"round",colour:"red")
static let Orange = (shape:"round",colour:"orange")
static let Banana = (shape:"long",colour:"yellow")
}
Then I have the following function:
static func PrintFruit(fruit:FruitTuple)
{
let shape:String = fruit.shape
let colour:String = fruit.colour
print("Fruit is \(shape) and \(colour)")
}
On the fruit.shape
and fruit.colour
I get the error:
Value of type 'FruitTuple' has no member 'shape'
Fair enough, so I alter the enum to have a type:
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")
}
But then on the enum declaration I get the error:
Inheritance from non-named type '(shape: String, colour: String)'
So, the question is: Is it even possible to have a tuple in an enum and be able to reference it's component parts in such a way? Am I just missing something fundamental here?
Upvotes: 3
Views: 2546
Reputation: 3499
As pointed out by @MartinR. Also, according to Apple docs, "enumeration cases can specify associated values of any type to be stored along with each different case value". If you want to keep using enum
, you may need to do something like:
static func PrintFruit(fruit:FruitTuple.Apple)
{
let shape:String = fruit.shape
let colour:String = fruit.colour
print("Fruit is \(shape) and \(colour)")
}
I'm not certain of what you want, but I guess using typealias
could help achieve your goal.
typealias FruitTuple = (shape: String, colour: String)
enum Fruit
{
static let apple = FruitTuple("round" , "red")
static let orange = FruitTuple("round", "orange")
static let banana = FruitTuple("long", "yellow")
}
func printFruit(fruitTuple: FruitTuple)
{
let shape:String = fruitTuple.shape
let colour:String = fruitTuple.colour
}
Upvotes: 5