
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Enum Value to String in Swift
In Swift, you can convert an enum value to a String through the rawValue property. This is if the enum has a raw value of type String. If the enum doesn't have a raw value, you can use the String(describing:) initializer to get a string representation of the enum value. Also, you can use the CustomStringConvertible protocol.
Example 1
Convert an enum value to a string using the rawValue property.In this example, Fruit is an enum with a raw value of type String. The rawValue property is used to get a string representation of the myFruit enum value, which is "Apple".
import Foundation enum Fruit: String { case apple = "Apple" case banana = "Banana" case orange = "Orange" } let myFruit = Fruit.apple let fruitString = myFruit.rawValue print("Enum value: \(fruitString)")
Output
Enum value: Apple
Example 2
If the enum doesn't have a raw value, you can use the String(describing:) initializer to get a string representation of the enum value.In this example, Direction is an enum without a raw value. The String(describing:) initializer is used to get a string representation of the myDirection enum value, which is "north".
import Foundation enum Direction { case north case south case east case west } let myDirection = Direction.north let directionString = String(describing: myDirection) print("Enum value: \(directionString)")
Output
Enum value: north
Example 3
Customizing the String Representation of an Enum.In this example, we have an enum called Suit that implements the CustomStringConvertible protocol. We've provided a custom implementation of the description property that returns the appropriate symbol for each suit. When we create an instance of Suit and print it, the custom string representation is used.
import Foundation enum Suit: CustomStringConvertible { case spades, hearts, diamonds, clubs var description: String { switch self { case .spades: return "??" case .hearts: return "??" case .diamonds: return "??" case .clubs: return "??" } } } let suit = Suit.hearts print(suit)
Output
??
Conclusion
In conclusion, there are several ways to convert an enum value to a string in Swift ?
The rawValue property may be used to retrieve the string representation of an enum value if it has a raw value of type String.
You can implement the CustomStringConvertible protocol and offer your own implementation of the description property or method. This is if you wish to customize the string representation of your enum item.
Use the String(describing:) initializer to obtain a default string representation of the enum value if it lacks a raw value and you don't want to supply a specific string representation.
Based on the demands of your specific use case, select the strategy that best meets your needs.