How to get the name of enumeration value in Swift?

前端 未结 12 1447
太阳男子
太阳男子 2020-11-29 23:53

If I have an enumeration with raw Integer values:

enum City: Int {
  case Melbourne = 1, Chelyabinsk, Bursa
}

let city = City.Melbourne
         


        
12条回答
  •  攒了一身酷
    2020-11-30 00:32

    For Swift:

    extension UIDeviceBatteryState: CustomStringConvertible {
    
        public var description: String {
            switch self {
            case .unknown:
                return "unknown"
            case .unplugged:
                return "unplugged"
            case .charging:
                return "charging"
            case .full:
                return "full"
            }
        }
    
    }
    

    if your variable "batteryState" then call:

    self.batteryState.description
    

提交回复
热议问题