Getting Name of ObjC Enum in Swift 3?

前端 未结 3 1946
野的像风
野的像风 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: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"
            }
        }
    }
    

提交回复
热议问题