打印UIInterfaceOrientation类型变量值的最简单方法是什么?

时间:2017-03-11 08:22:39

标签: swift xcode debugging xcode-debugger

在调试我的应用时,我想打印出orien类型的局部变量UIInterfaceOrientation的值。

我试过了print("\(orien"),但是打印出来了:

UIInterfaceOrientation

......这显然没用。

然后我尝试dump(orien),这产生了另一个无用的输出:

- __C.UIInterfaceOrientation

在Xcode中,我设置了一个断点并右键单击了该变量并选择了Print Description of,这产生了:

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

我最后写了:

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")
    }
  }
}

有更好的解决方案吗?

顺便说一句,这个问题也发生在CGFloat上 - XCode的调试器将其打印为<variable not available>

1 个答案:

答案 0 :(得分:1)

你不能只打印枚举案例的rawValue吗?显然,这是不可能的,因为它返回一个Int,因为UIInterfaceOrientation是Int的枚举。

编辑:以下代码可以提供帮助,因为它使用变量创建描述。

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"
    }
}
}

添加完成后,您可以通过以下方式使用description

UIInterfaceOrientation.landscapeLeft.description

landscapeLeft