Home »
Swift »
Swift Programs
Swift program to create an enumeration with the specific type and access its raw value
Here, we are going to learn how to create an enumeration with the specific type and access its raw value in Swift programming language?
Submitted by Nidhi, on July 09, 2021
Problem Solution:
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.
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 Colors, Countries 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.
Swift Enum Programs »