Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting String name of Objective-C @objc enum value in Swift?

If an Objective-C function returns a status value with enum, is there a way to get the string of the enum in Swift?

The same question is asked of native Swift enumeration cases, but in this question I am specifically working with @objc enum Objective-C enums in Swift:

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.

like image 415
jl303 Avatar asked Nov 27 '16 12:11

jl303


People also ask

Can I use Swift enum in Objective C?

From what I understand, you can only import Swift stuff in . m files and there is no way to forward declare an enum in Objective C.

How do you name an enum in Swift?

The name of an enum in Swift should follow the PascalCase naming convention in which the first letter of each word in a compound word is capitalized. The values declared in an enum — up , down , left and right — are referred to as enumeration case. We use the case keyword to introduce a new enumeration case.

How do you declare an enum in Objective C?

Enums are defined by the following the syntax above. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA, MyEnumValueB, MyEnumValueC, }; You also can set your own raw-values to the enumeration types. typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, };

Can associated values and raw values coexist in Swift enumeration?

The Problem with Associated Values We had to do this because Swift doesn't allow us to have both: raw values and associated values within the same enum. A Swift enum can either have raw values or associated values.


2 Answers

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"
        }
    }
}
like image 117
David James Avatar answered Oct 24 '22 11:10

David James


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

like image 23
Itai Ferber Avatar answered Oct 24 '22 11:10

Itai Ferber