Getting the name ObjC Enum in Swift 3?

If the ObjC function returns an enumerated state value, is there a way to get an enumeration string in Swift 3? If I do debugPrint("\(status)") or print("\(status)") , I just get the enumeration name instead of the value. If I do status.rawValue , I get int, but that does not mean that it needs to be interpreted.

+6
source share
3 answers

Objective-C enum case names do not exist at run time - they are just integer values, unlike Swift enum s, which have run-time information associated with them. If you want the names of individual cases to be executed at run time, you will have to store them separately and access them through integer values ​​(i.e., translate from an int value into a name recognizable by humans).

+3
source

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

Be very careful with these results. They are not localized and are not promised to remain consistent between releases. They are not a serialization API. There is no promise that they can be canceled to provide you with the original data. The result of these enumeration methods has changed dramatically in Swift 3. I absolutely expect them to change again, since Foundation will be enhanced for Swift.

I do not want to scare you about these methods. String(describing:) pretty well defined as to what it returns in some cases (especially user types that implement certain protocols), but not in all cases. You must read the documents and use reasonable care. String(reflecting:) , on the other hand, is clearly "suitable for debugging." I would not argue about this line.

+2
source

You can also add an Obj-C enum CustomStringConvertible to CustomStringConvertible and translate the values ​​into strings this way. Until you use default , you will be warned if any of these values ​​change in future versions.

For instance:

 extension NSLayoutAttribute : CustomStringConvertible { public var description: String { switch self { case .left : return "left" case .right : return "right" case .top : return "top" case .bottom : return "bottom" case .leading : return "leading" case .trailing : return "trailing" case .width : return "width" case .height : return "height" case .centerX : return "centerX" case .centerY : return "centerY" case .lastBaseline : return "lastBaseline" case .firstBaseline : return "firstBaseline" case .leftMargin : return "leftMargin" case .rightMargin : return "rightMargin" case .topMargin : return "topMargin" case .bottomMargin : return "bottomMargin" case .leadingMargin : return "leadingMargin" case .trailingMargin : return "trailingMargin" case .centerXWithinMargins : return "centerXWithinMargins" case .centerYWithinMargins : return "centerYWithinMargins" case .notAnAttribute : return "notAnAttribute" } } } 
+2
source

Source: https://habr.com/ru/post/1012684/


All Articles