swiftxcodedebuggingxcode-debugger

What's the easiest way to print the value of a variable of type UIInterfaceOrientation?


As I debug my app, I want to print out the value of a local variable orien of type UIInterfaceOrientation.

I tried print("\(orien") but it printed:

UIInterfaceOrientation

... which is obviously useless.

I then tried dump(orien), which produced another useless output:

- __C.UIInterfaceOrientation

In Xcode, I set a breakpoint and right-clicked the variable and chose Print Description of, which produced:

Printing description of orien:
(UIInterfaceOrientation) orien = <variable not available>

I ended up writing:

extension UIInterfaceOrientation {
  func dump() {
    switch self {
    case .portrait: print("Interface orientation is Portrait")
    case .portraitUpsideDown: print("Interface orientation is Portrait upside down")
    case .landscapeLeft: print("Interface orientation is Landscape left")
    case .landscapeRight: print("Interface orientation is Landscape right")
    case .unknown: print("Interface orientation is unknown")
    }
  }
}

Is there a better solution?

BTW, this problem also occurred with a CGFloat — XCode's debugger printed it out as <variable not available>.


Solution

  • Can't you just print the rawValue of the enum case? Apparently, this isn't possible because it returns a Int because UIInterfaceOrientation is a enum of Int.

    EDIT: The following code could help because it creates a description, using a variable.

    extension UIInterfaceOrientation {
    public var description: String {
        switch self {
        case .landscapeLeft: return "landscapeLeft"
        case .landscapeRight: return "landscapeRight"
        case .portrait: return "portrait"
        case .portraitUpsideDown: return "portraitUpsideDown"
        case .unknown: return "unknown"
        }
    }
    }
    

    After this addition, you can use description in the following way:

    UIInterfaceOrientation.landscapeLeft.description
    
    landscapeLeft