简体   繁体   English

枚举的原始值,类/结构的默认值,有什么不同?

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

In Swift, there is Raw Value in Enumeration and Default Value in class and structure. 在Swift中,枚举中包含原始值 ,而类和结构中包含默认值 What's the different? 有什么不同? Can someone explain that for me? 有人可以帮我解释一下吗?

Ex. 例如 of Raw Values of Enumeration (From the Office Swift Document) 枚举的原始值(来自Office Swift文档)

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

From Apple docs : 来自Apple文档

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. 作为关联值的替代方法,枚举案例可以预先填充默认值(称为原始值),这些默认值都是相同的类型。

So I guess it is the same. 所以我想是一样的。

On the other hand, with "default value", you may be referring to the default value of an enum case where no values have been set, for example: 另一方面,对于“默认值”,您可能是指未设置任何值的枚举情况的默认值,例如:

enum TestEnum: Int  {    
    case A
    case B    
}

Here, TestEnum.A has a default value of 0 , and TestEnum.B has a default value of 1 . 在这里, TestEnum.A的默认值为0 ,而TestEnum.B的默认值为1

Raw value refers to the actual value of an enum case (in the enum's type, in this example it would be Int ): 原始值是指枚举实例的实际值(在枚举类型中,在此示例中为Int ):

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

Here, TestEnum.A has the default value (which is also the raw value) of 0 , and TestEnum.B has a raw value of 3 (which is no longer the default value). 在这里, TestEnum.A的默认值(也是原始值)为0 ,而TestEnum.B的原始值是3 (不再是默认值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM