The source code to demonstrate enumeration is given below. The given program is compiled and executed successfully.
// Swift program to demonstrate enum
import Swift
enum Colors {
case RED
case GREEN
case BLUE
case WHITE
}
enum Fruits {
case MANGO, APPLE, BANANA, PAYAYA
}
print("Colors: ",Colors.RED,Colors.GREEN,Colors.BLUE,Colors.WHITE)
print("Fruits: ",Fruits.MANGO,Fruits.APPLE,Fruits.BANANA,Fruits.PAYAYA)
Output:
Colors: RED GREEN BLUE WHITE
Fruits: MANGO APPLE BANANA PAYAYA
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
import Swift
Here, we created two enumerations Colors, Fruits. In the Colors enumeration, we created each constant with a different case keyword. While we create constants using a single case in Fruits enumeration. After that, we printed the values of enumeration constants on the console screen.
Program/Source Code:
The source code to demonstrate enumeration is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
Here, we created two enumerations Colors, Fruits. In the Colors enumeration, we created each constant with a different case keyword. While we create constants using a single case in Fruits enumeration. After that, we printed the values of enumeration constants on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer