swift 中的嵌套枚举 - 基于枚举的编译和函数
Nested enums in swift - compering and functions based on enum
使用 ==
运算符和用于打印的测试函数创建了嵌套枚举:
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)) |")
应该return类似
| true __ MDI | //or "| false __ PSC |"
但我得到的是:
PSC
| false __ () |
或
MDI
| true __ () |
我不知道这里发生了什么,刚开始学习 Swift。有小费吗 ?谢谢。
printX
不会 return 任何东西,但它会打印到控制台。所以当你这样称呼时:
print("| \(x==y) __ \(printX(hs: x)) |")
它在为 print 语句构建字符串时计算 printX(hs: x)
。 returns Void
,在最后的字符串中显示为 ()
,但在执行此操作时,它会写入控制台,因此您会在行中看到值 MDI 或 PSC之前。
你可以只做print(x)
,它会给你一个合理的调试描述:
modifyImage(Untitled.OperationType.ModificationType.none)
(无标题是这里的模块标识符)或者你可以让你的类型实现 CustomDebugStringConvertible
和 return 在 debugDescription
.
中更有用的东西
使用 ==
运算符和用于打印的测试函数创建了嵌套枚举:
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)) |")
应该return类似
| true __ MDI | //or "| false __ PSC |"
但我得到的是:
PSC
| false __ () |
或
MDI
| true __ () |
我不知道这里发生了什么,刚开始学习 Swift。有小费吗 ?谢谢。
printX
不会 return 任何东西,但它会打印到控制台。所以当你这样称呼时:
print("| \(x==y) __ \(printX(hs: x)) |")
它在为 print 语句构建字符串时计算 printX(hs: x)
。 returns Void
,在最后的字符串中显示为 ()
,但在执行此操作时,它会写入控制台,因此您会在行中看到值 MDI 或 PSC之前。
你可以只做print(x)
,它会给你一个合理的调试描述:
modifyImage(Untitled.OperationType.ModificationType.none)
(无标题是这里的模块标识符)或者你可以让你的类型实现 CustomDebugStringConvertible
和 return 在 debugDescription
.