枚举的原始值,class/structure 的默认值,有什么不同?

Raw Value of Enumeration, Default value of a class/structure, What's the different?

在Swift中,枚举中有原始值,class和结构中有默认值。有什么不同?有人可以为我解释一下吗?

例如。枚举的原始值(来自 Office Swift 文档)

enum ASCIIControlCaracter: Character {
    case Tab = "\t"
    case LineFeed = "\n"
    case CarriageReturn = "\r"
}

来自 Apple docs

Raw Values

The barcode example in Associated Values shows how cases of an enumeration can declare that they store associated values of different types. As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type.

所以我猜是一样的。

另一方面,对于 "default value",您可能指的是未设置任何值的枚举情况的默认值,例如:

enum TestEnum: Int  {    
    case A
    case B    
}

这里,TestEnum.A的默认值为0TestEnum.B的默认值为1

原始值是指枚举案例的实际值(在枚举类型中,在本例中为Int):

enum TestEnum: Int  {    
    case A
    case B = 3   
}

这里,TestEnum.A的默认值(也是原始值)为0TestEnum.B的原始值为3(即不再是默认值)。