Complete the code to make the enum conform to CaseIterable.
enum Direction: [1] {
case north, south, east, west
}To iterate over all enum cases, the enum must conform to CaseIterable.
Complete the code to print all cases of the enum.
for direction in Direction.[1] { print(direction) }
The CaseIterable protocol provides the allCases property to access all enum cases.
Fix the error in the code to correctly iterate over enum cases.
enum Color: CaseIterable {
case red, green, blue
}
for color in Color.[1]() {
print(color)
}allCases with parentheses like a function.allCases is a property, not a method, so it should not have parentheses.
Fill both blanks to create a dictionary with enum case names as keys and raw values as values.
enum Fruit: String, CaseIterable {
case apple = "Red"
case banana = "Yellow"
case grape = "Purple"
}
let fruitColors = [[1]: [2] for fruit in Fruit.allCases]fruit.name or fruit.description which do not exist.Use the enum case itself as the key and its rawValue as the value.
Fill all three blanks to filter enum cases with raw value length greater than 5 and create a list of their names.
enum Vehicle: String, CaseIterable {
case car = "Auto"
case motorcycle = "Motorcycle"
case bicycle = "Bike"
case airplane = "Airplane"
}
let longNames = [[1] for vehicle in Vehicle.allCases if [2].[3] > 5]We want the raw value names where the length is greater than 5. So we use vehicle.rawValue in the list and check vehicle.rawValue.count > 5.