使用==運算子和測驗函式創建了一個嵌套列舉以 進行列印:
enum OperationType{
case presentController(_ controller: ControllerType)
enum ControllerType{
case imagePickerVC, histogramVC, faceDetectionVC, cellDetectionVC,none
}
case modifyImage(_ modificationType: ModificationType)
enum ModificationType{
case saveImage, deleteImage,none
}
}
extension OperationType: Equatable{
static func ==(lhs: OperationType,rhs: OperationType)->Bool{
switch (lhs,rhs) {
case (.presentController,.presentController):
return true//lhs_controller == rhs_controller
case (.modifyImage,.modifyImage):
return true//lhs_modificationType == rhs_modificationType
default:
return false
}
}
}
//test func to print
func printX(hs: OperationType){
switch(hs) {
case .presentController:
print("PSC")
break
case .modifyImage:
print("MDI")
break
default:
break
}
}
在列印一些結果時(來自一個 ViewController 的邏輯):
let x:OperationType = menuData[indexPath.section].Operations[indexPath.row1].Operation
let y:OperationType = .modifyImage(.none)
print("| \(x==y) __ \(printX(hs: x)) |")
它應該回傳類似
| true __ MDI | //or "| false __ PSC |"
但我得到的是:
PSC
| false __ () |
或者
MDI
| true __ () |
我不知道這里發生了什么,不久前開始學習 Swift。有小費嗎 ?謝謝。
uj5u.com熱心網友回復:
printX不回傳任何內容,但會列印到控制臺。所以當你這樣稱呼時:
print("| \(x==y) __ \(printX(hs: x)) |")
printX(hs: x)它在為 print 陳述句構建字串時進行評估。回傳Void,它()在最終字串中出現,但在執行此操作時,它確實會寫入控制臺,因此您會在前面的行中看到值 MDI 或 PSC。
你可以這樣做print(x),它會給你一個合理的除錯描述:
modifyImage(Untitled.OperationType.ModificationType.none)
(無標題是這里的模塊識別符號)或者你可以讓你的型別實作CustomDebugStringConvertible并回傳一些更有用的debugDescription.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416818.html
標籤:
