- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get the name of the enumeration value in Swift?
In Swift, you can conform to the CustomStringConvertible protocol to provide a default name for each case in an enumeration. This protocol can be used to provide custom meaning as per the requirement.
CustomStringConvertible
CustomStringConvertible is a protocol in Swift that defines a single property, description, which returns a String representation of an instance of a conforming type. By conforming to CustomStringConvertible, you can customize how your types are represented as strings when they are printed, logged, or otherwise converted to a string.
When you conform to CustomStringConvertible, you define how instances of your type are represented as strings by implementing the description property. The description property returns a String that represents the contents of the instance in a human-readable format. By default, the description property returns the name of the type, but you can customize this by providing your implementation.
Example 1
Here's an example of how to use CustomStringConvertible with an enumeration −
enum Direction: CustomStringConvertible { case north case south case east case west var description: String { switch self { case .north: return "North" case .south: return "South" case .east: return "East" case .west: return "West" } } } let direction = Direction.north print("The direction is \(direction)")
Output
The direction is North
In the above example, we define an enumeration Direction with four cases: north, south, east, and west. We also declare that Direction conforms to the CustomStringConvertible protocol.
We then implement the description property, which returns a String representation of the enumeration value. Inside the implementation of the description property, we use a switch statement to return the appropriate string value based on the enumeration case.
Finally, we create a variable direction of type Direction and set it to Direction.north. We use string interpolation to print out a message that includes the value of direction. Since Direction conforms to CustomStringConvertible, the description property will be used to convert the Direction enumeration value to a string representation, which will be "North" in this case. This string will be included in the printed message.
You can use this same approach to customize the string representation of any enumeration value that conforms to the CustomStringConvertible protocol.
Example 2
Here's an example of how to use CustomStringConvertible with a network error code enum −
enum NetworkErrorCode: Int, CustomStringConvertible { case noInternet = -1009 case timeout = -1001 case badRequest = 400 case unauthorized = 401 case forbidden = 403 case notFound = 404 var description: String { switch self { case .noInternet: return "No Internet Connection" case .timeout: return "Request Timed Out" case .badRequest: return "Bad Request" case .unauthorized: return "Unauthorized" case .forbidden: return "Forbidden" case .notFound: return "Resource Not Found" } } var code: Int { return self.rawValue } } let error = NetworkErrorCode.timeout print("Error: \(error.code) - \(error)")
Output
Error: -1001 - Request Timed Out
In the above example, we define an enumeration NetworkErrorCode that represents different types of network errors. Each case of the enumeration has a corresponding integer value that represents an error code.
We also declare that NetworkErrorCode conforms to the CustomStringConvertible protocol. We implement the description property, which returns a String that describes the error represented by the enumeration case.
We also define a computed property code that returns the integer value of the enumeration case.
Finally, we create a variable error of type NetworkErrorCode and set it to .timeout. We use string interpolation to print out a message that includes the error code and description of the error.
Since NetworkErrorCode conforms to CustomStringConvertible, the description property will be used to convert the enumeration value to a string representation, which will be "Request Timed Out" in this case. The output will be "Error: -1001 - Request Timed Out".
Using CustomStringConvertible in this way allows us to create a more user-friendly representation of network error codes that can be used in logs, error messages, and other types of output.
Conclusion
In conclusion, CustomStringConvertible is a Swift protocol that allows us to customize the string representation of a type. By conforming to this protocol, we can define a description property that returns a string that represents the value of the type in a human-readable format.
This can be particularly useful for creating custom error types or enumeration values, where we want to provide more descriptive error messages or make the values more user-friendly when outputting to a console or log. By providing a custom string representation, we can make it easier for developers to understand what's going on in the code and for users to understand what's happening in the application.