Q:

Swift program to demonstrate the enumeration

belongs to collection: Swift Enum Programs

0

Here, we will create two enumerations using the enum keyword and print their values on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

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 ColorsFruits. 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

total answers (1)

Swift program to create an enumeration with the sp... >>