Какой самый простой способ вывести значение переменной типа UIInterfaceOrientation?
Когда я отлаживаю свое приложение, я хочу распечатать значение локальной переменной 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 ответ
Вы не можете просто напечатать rawValue регистра enum? По-видимому, это невозможно, потому что он возвращает 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