XuDong Wu XuDong Wu -4 years ago 98
Swift Question

Why can I not print an enum's case name in Swift 3?

I have a enum in Swift 3, I can print its name using following code:

enum definition

public enum Foo : Int {
case F1
}


following code will get
F1
in console

print(Foo.F1)


But when I try to print enums defined in Charts

I can only get
HorizontalAlignment
in console

print(Legend.HorizontalAlignment.center)


Why is that?

Answer Source

Because the enum is declared as @objc. In Legend.swift:

@objc(ChartLegendHorizontalAlignment)
public enum HorizontalAlignment: Int
{
    case left
    case center
    case right
}

Try it yourself, add @objc in front of your Foo enum and you will see that Foo is printed. While I'm not exactly sure why this happens, it's likely it's due to the way @objc changes an enum's debug description. An @objc enum prints its type, while a Swift enum prints its case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download