How to get the name of enumeration value in Swift? -
if have enumeration raw integer
values:
enum city: int { case melbourne = 1, chelyabinsk, bursa } let city = city.melbourne
how can convert city
value string melbourne
? kind of type name introspection available in language?
something (this code not work):
println("your city \(city.magicfunction)") > city melbourne
as of xcode 7 beta 5 can print type names , enum cases default using print(_:)
, or convert string
using string
's init(_:)
initializer or string interpolation syntax. example:
enum city: int { case melbourne = 1, chelyabinsk, bursa } let city = city.melbourne print(city) // prints "melbourne" let cityname = "\(city)" // or `let cityname = string(city)` // cityname contains "melbourne"
so there no longer need define & maintain convenience function switches on each case return string literal. in addition, works automatically enum, if no raw-value type specified.
debugprint(_:)
& string(reflecting:)
can used fully-qualified name:
debugprint(city) // prints "app.city.melbourne" (or similar, depending on full scope) let citydebugname = string(reflecting: city) // citydebugname contains "app.city.melbourne"
note can customise printed in each of these scenarios:
extension city: customstringconvertible { var description: string { return "city \(rawvalue)" } } print(city) // prints "city 1" extension city: customdebugstringconvertible { var debugdescription: string { return "city (rawvalue: \(rawvalue))" } } debugprint(city) // prints "city (rawvalue: 1)"
(i haven't found way call "default" value, example, print "the city melbourne" without resorting switch statement. using \(self)
in implementation of description
/debugdescription
causes infinite recursion.)
comments above string
's init(_:)
& init(reflecting:)
initializers describe printed, depending on reflected type conforms to:
extension string { /// initialize `self` textual representation of `instance`. /// /// * if `t` conforms `streamable`, result obtained /// calling `instance.writeto(s)` on empty string s. /// * otherwise, if `t` conforms `customstringconvertible`, /// result `instance`'s `description` /// * otherwise, if `t` conforms `customdebugstringconvertible`, /// result `instance`'s `debugdescription` /// * otherwise, unspecified result supplied automatically /// swift standard library. /// /// - seealso: `string.init<t>(reflecting: t)` public init<t>(_ instance: t) /// initialize `self` detailed textual representation of /// `subject`, suitable debugging. /// /// * if `t` conforms `customdebugstringconvertible`, result /// `subject`'s `debugdescription`. /// /// * otherwise, if `t` conforms `customstringconvertible`, result /// `subject`'s `description`. /// /// * otherwise, if `t` conforms `streamable`, result /// obtained calling `subject.writeto(s)` on empty string s. /// /// * otherwise, unspecified result supplied automatically /// swift standard library. /// /// - seealso: `string.init<t>(t)` public init<t>(reflecting subject: t) }
see release notes info change.
Comments
Post a Comment