将一个enum Type赋给另一个enum Type



如何将一种枚举类型的变量类型转换为另一种枚举类型。我有两个Int Enum。当我创建一个类型为第一枚举的变量并从第二枚举中赋值时,它不允许我。我们如何才能做到这一点?

下面是代码
enum Sections: Int {
    case initiateZero = 0
    case Air
    case Weight
}
enum SubSections: Int {
    case MaxPath = 0
    case Weight
    case FullWeight
}

为得到它们。

var section = Sections.initiateZero
if isLongForm {
    section = Sections(rawValue: indexPath.section)!
} else {
    section = SubSections(rawValue: indexPath.section)
}

无法分配它们。请建议。

我想不出有效的用例,但是您可能想使用它的rawValue s:

enum Enum1: Int {
    case one = 0
    case two = 1
}
enum Enum2: Int {
    case one = 0
    case two = 1
}
let enum1 = Enum1.one
let enum2 = Enum2(rawValue: enum1.rawValue)
print(enum2)

打印:

Optional(Enum2.one)

最新更新