gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 15:47:53 25 4
gpt4 key购买 nike

当我调试我的应用程序时,我想打印出局部变量的值 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>。 .

最佳答案

你不能只打印枚举案例的 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

关于swift - 打印 UIInterfaceOrientation 类型变量值的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42733026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com