在Swift中,我为什么要创建一个新变量并将其设置为枚举?

时间:2021-01-07 00:09:05

EDITED I'm fairly new to swift. I'm trying to understand the following code:

编辑我对swift很新。我正在尝试理解以下代码:

struct PersonMovement {
  enum Arms: Int {
    case Bent
    case Straight

    func moreBent() -> Arms {
      return Arms(bentDegree: rawValue - 1) ?? .Straight
    ....
  }
  enum Legs: Int {
    case Bent
    case....
  }
  enum Head: Int {
    ....
  }

  var arms: Arms
  var legs: Legs
  var head: Head
}

There is another swift file that uses the variables arms, legs and head. I'm wondering why those separate variables were created in the first place. Why wouldn't the other swift file simply point to the enums Arms, Legs and Head themselves.

还有另一个swift文件使用变量arm,legs和head。我想知道为什么首先创建这些单独的变量。为什么其他快速文件不会简单地指向枚举武器,腿和头本身。

Is it because you must ALWAYS create a separate variable with the enum's value if you wish to make use of the enum? Or is it because it is nested and not accessible globally? Again, I'm a newbie here. Sorry for not being clear. Also my second time posting here. Apologies

是因为如果你想使用枚举,你必须总是用枚举值创建一个单独的变量吗?或者是因为它是嵌套的而且不能全局访问?再说一次,我是新手。对不起,不清楚。我也是第二次在这里发帖。道歉

1 个解决方案

#1


0  

If i understand your question correctly you are mixing up variables with types.

如果我正确理解你的问题,你就是把变量和类型混在一起。

You define instances of variables with the enum types of Arms, Legs and Head.

您可以使用Arms,Legs和Head的枚举类型定义变量的实例。

Those instances can then be defined the different enum values, for example:

然后可以将这些实例定义为不同的枚举值,例如:

arms can be either Bent or Straight.

武器可以是弯曲的也可以是直的。

#1


0  

If i understand your question correctly you are mixing up variables with types.

如果我正确理解你的问题,你就是把变量和类型混在一起。

You define instances of variables with the enum types of Arms, Legs and Head.

您可以使用Arms,Legs和Head的枚举类型定义变量的实例。

Those instances can then be defined the different enum values, for example:

然后可以将这些实例定义为不同的枚举值,例如:

arms can be either Bent or Straight.

武器可以是弯曲的也可以是直的。