enum string swift example

Example: enum string swift example

enum Alert: CustomStringConvertible {
    case DuplicateUserNameMessage(userName: String)
    case NetworkFailureMessage(url: String)
    case OverheatingMessage(degrees: Float)
    var description: String {
        switch self {
        case .DuplicateUserNameMessage(let userName): 
          return "The username \(userName) already exists"
        case .NetworkFailureMessage(let url): 
          return "Unable to connect to \(url)"
        case .OverheatingMessage(let degrees):
          return String(format: "Warning: The water is %.1f degrees", degrees)
        }
    }
}

let userName = "Mike"
let url = "www.google.com"
let degrees: Float = 103.4587254

print(Alert.DuplicateUserNameMessage(userName: userName).description)
print(Alert.NetworkFailureMessage(url: url).description)
print(Alert.OverheatingMessage(degrees: degrees).description)

Tags:

Misc Example