Getting Name of ObjC Enum in Swift 3?

If an ObjC function returns a status value with enum, is there way to get the string of the enum in Swift 3? If I do debugPrint("\(status)"), or print("\(status)") I just get the name of the enum instead of value. If I do status.rawValue, I get the int, but it doesn't mean much to interpret.


ANSWERS:


The names of Objective-C enum cases do not exist at runtime — they are simply integer values, unlike Swift's enums, which have runtime information associated with them. If you want the names of the individual cases at runtime, you will have to store them separately and access them via the integer values (i.e. translate from the int value to a human-recognizable name).


If print(x) gives you what you want, then use String(describing:). This is almost always the correct answer. (Generally speaking, this will be equivalent to "\(x)". They don't have to be the same, but I haven't found a case yet where they're not.) If only debugPrint() gives you what you want, then use String(reflecting:).

Be very careful with these results. They are not localized and not promised to remain consistent between releases. They are not a serialization API. There's no promise they can be reversed to give you the original data. The output of these methods for enums changed dramatically in Swift 3. I absolutely expect them to change again as Foundation is refined further for Swift.

I don't want to over-scare you about these methods. String(describing:) is pretty well defined about what it returns in some cases (particularly custom types that implement certain protocols), but not in all cases. You have to read the docs and have reasonable caution around it. On the other hand String(reflecting:) is explicitly "suitable for debugging." I wouldn't bet on anything about this string.



 MORE:


 ? How to generate dynamic columns in JSF Datatable?
 ? NPE Argument Error: Parameter value is null (JSF) only when <h:head> is in page
 ? what are the minimal JSF jars needed to deploy a JSF app with Tomcat
 ? Printing a dynamic 2D array in C
 ? Dynamic printing array
 ? Using angular2. Dynamic event handling in real time
 ? Using angular2. Dynamic event handling in real time
 ? Using angular2. Dynamic event handling in real time
 ? Angular2 use of onmouseup event overwrites native behavior
 ? Angular2 + Laravel with Real time & WebSockets