Getting Name of ObjC Enum in Swift 3?

前端 未结 3 1833
野的像风
野的像风 2021-01-05 10:55

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(

相关标签:
3条回答
  • 2021-01-05 11:12

    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.

    0 讨论(0)
  • 2021-01-05 11:26

    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).

    0 讨论(0)
  • 2021-01-05 11:33

    You can also add conformance of the Obj-C enum to CustomStringConvertible and translate values to strings that way. As long as you don't use default you will be warned if any of these values change in future versions.

    For example:

    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"
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题