Swift program to demonstrate the enumeration

Here, we are going to demonstrate the enumeration in Swift programming language.
Submitted by Nidhi, on July 09, 2021

Problem Solution:

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

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

Swift Enum Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.