枚举的原始值,类/结构的默认值,有什么不同? [英] Raw Value of Enumeration, Default value of a class/structure, What's the different?

查看:109
本文介绍了枚举的原始值,类/结构的默认值,有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Swift, there is Raw Value in Enumeration and Default Value in class and structure. What's the different? Can someone explain that for me?

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

Ex. of Raw Values of Enumeration (From the Office Swift Document)

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


推荐答案

来自 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.

所以我想也是一样。

另一方面,对于默认值,您可能是指没有设置任何值的枚举案例的默认值,例如:

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    
}

此处, TestEnum .A 的默认值为 0 ,而 TestEnum.B 的默认值为 1

Here, TestEnum.A has a default value of 0, and TestEnum.B has a default value of 1.

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

Raw value refers to the actual value of an enum case (in the enum's type, in this example it would be Int):

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

这里, TestEnum.A 0 的默认值(也是原始值),而 TestEnum.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).

这篇关于枚举的原始值,类/结构的默认值,有什么不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆