Created a nested enum with == operator and test function for printing:
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
}
}
When it comes to printing some result ( logic from one ViewController) :
let x:OperationType = menuData[indexPath.section].Operations[indexPath.row1].Operation
let y:OperationType = .modifyImage(.none)
print("| \(x==y) __ \(printX(hs: x)) |")
It should return something like
| true __ MDI | //or "| false __ PSC |"
But what i get is:
PSC
| false __ () |
OR
MDI
| true __ () |
I don't have any clue what's going on here, started learning Swift while ago. Any tips ? Thanks.
CodePudding user response:
printX doesn't return anything, but it does print to the console. So when you call this:
print("| \(x==y) __ \(printX(hs: x)) |")
it evaluates printX(hs: x) while it is building the string for the print statement. That returns Void, which comes out as () in the final string, but while doing that, it does write to the console, so you see the value MDI or PSC on the line before.
You can just do print(x) and it will give you a reasonable debug description:
modifyImage(Untitled.OperationType.ModificationType.none)
(Untitled is the module identifier here) or you can make your types implement CustomDebugStringConvertible and return something more useful in debugDescription.
