我试着用枚举对物品进行分类。因此,我的最终结果是在枚举中有枚举。我不太清楚这件事是怎么发生的。
但是我想要做的是点击枚举MaterialClassification,然后如果有第二个枚举,就像万一黏土一样,点击该枚举的值,将其作为字符串返回。
enum MaterialClassification {
case clay(value: Clay)
case flux//(value: Fluxs)
case glassFormer
case stain//(value: Clay)
case accessoryMaterial
case all//(value: Clay)
}
extension MaterialClassification {
var materiaIdentifier: String {
switch self {
case .clay:
return "clay"
case .flux:
return "flux"
case .glassFormer:
return "glassFormer"
case .stain:
return "stain"
case .accessoryMaterial:
return "accessoryMaterial"
case .all:
return "all"
}
}
}enum Clay {
case iskaolin// = "Kaolin"
case isPrimaryKaolin// = "Primary Kaolin"
case isSecondaryKaolin //= "Secondary Kaolin"
case isBallClay //= "Ball Clay"
case isStoneware// = "Stoneware"
case isFireClay //= "Fire Clay"
case isEarthenware// = "Earthenware"
case isVolcanicClay// = "Volcanic"
}
extension Clay {
var clayType: String {
switch self {
case .iskaolin:
return "Kaolin"
case .isPrimaryKaolin:
return "Primary Kaolin"
case .isSecondaryKaolin:
return "Secondary Kaolin"
case .isBallClay:
return "Ball Clay"
case .isStoneware:
return "Stoneware"
case .isFireClay:
return "Fire Clay"
case .isEarthenware:
return "Earthenware"
case .isVolcanicClay:
return "Volcanic Clay"
}
}
}我的目标是在需要时能够返回嵌套字符串。例如:
materialClassification: MaterialClassification.clay(type: Clay.isPrimaryKaolin)我需要一个归还“初级高岭土”的方法。但我不知道如何连接这两个枚举。
发布于 2020-06-29 00:25:09
如果我正确理解您的问题,您希望访问关联类型的属性。您可以向MaterialClassification枚举添加一个新属性,并使用它访问您的案例。
像这样的东西应该能起作用
var type: String? {
switch self {
case .clay(let clay):
return clay.clayType
case .flux(let flux):
return flux.fluxType
case .stain(let stain):
return stain.stainType
case .glassFormer, .accessoryMaterial, .all:
return nil
}
}https://stackoverflow.com/questions/62629552
复制相似问题