Trying to access the value of a double-nested enum in Swift that is passed in as a parameter to the first enum. I'm not sure how to access the CutStyle
; within the for
loop in shareSalad
, each ingredient
that is iterated over does not have a .dot
accessor except self
. I.e. there is no ingredient.cut
or ingredient(cut)
available.
I'm confused on how to access CutStyle
import Cocoa
enum Ingredients {
case lettuce(cut: CutStyle)
case tomatoes(cut: CutStyle)
case onions(cut: CutStyle)
case cucumbers(cut: CutStyle)
case dressing
enum CutStyle {
case diced
case chopped
case minced
case grated
}
}
var salad: [Ingredients] = []
func makeSalad(with ingredient: Ingredients) {
switch ingredient {
case .lettuce(let cut):
salad.append(.lettuce(cut: cut))
case .tomatoes(let cut):
salad.append(.tomatoes(cut: cut))
case .onions(let cut):
salad.append(.onions(cut: cut))
case .cucumbers(let cut):
salad.append(.cucumbers(cut: cut))
case .dressing(let cut):
salad.append(.dressing(cut: cut))
}
}
func shareSalad(my salad: [Ingredients]) {
print("My salad contains:")
for ingredient in salad {
// Q: How to access the ingredient CutStyle here?
print(ingredient)
}
}
makeSalad(with: .cucumbers(cut: .chopped))
makeSalad(with: .onions(cut: .diced))
makeSalad(with: .tomatoes(cut: .minced))
shareSalad(my: salad)
/*
My salad contains:
cucumbers(cut: __lldb_expr_29.Ingredients.CutStyle.chopped)
onions(cut: __lldb_expr_29.Ingredients.CutStyle.diced)
tomatoes(cut: __lldb_expr_29.Ingredients.CutStyle.minced)
*/