Q:

Swift program to create an enumeration with the specific type and access its raw value

belongs to collection: Swift Enum Programs

0

Here, we will create enumerations using the enum keyword for the specific type and print their raw values using the rawValue property 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 create enumeration with the specific type and access its raw value is given below. The given program is compiled and executed successfully.

// Swift program to create enumeration with
// the specific type and access its raw value

import Swift

enum Colors:Int {
    case RED    = 1
    case GREEN
    case BLUE
    case WHITE
}

enum Countries:String {
    case IN    = "India"
    case US    = "United State of America"
    case AU    = "Australia"
}

print("Colors: ",Colors.RED.rawValue,Colors.GREEN.rawValue,Colors.BLUE.rawValue,Colors.WHITE.rawValue)

print("Countries: ",Countries.IN.rawValue,",",Countries.US.rawValue,",",Countries.AU.rawValue)

Output:

Colors:  1 2 3 4
Countries:  India , United State of America , Australia

...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 ColorsCountries with types integer and string respectively. Then we accessed raw values of enum constants using the rawValue property and printed them 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 print all cases of enumeration... >>
<< Swift program to demonstrate the enumeration...