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

拟墨画扇 提交于 2021-02-11 09:41:47

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/42733026/whats-the-easiest-way-to-print-the-value-of-a-variable-of-type-uiinterfaceorien

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!