如何从切换嵌套枚举中返回一些枚举?

发布于 2025-02-10 16:37:32 字数 1822 浏览 1 评论 0原文

我是Swift的新手,并试图制作单元转换应用程序。 我使用可用的所有单位使用嵌套枚举:

protocol UnitProtocol {

}

enum Units: String, CaseIterable, UnitProtocol {
  case temperature, length, time, volume
  enum Temperature: String, CaseIterable, UnitProtocol {
    case celsius, fahrenheit, kelvin
  }
  enum Length: String, CaseIterable, UnitProtocol {
    case meters, kilometers, feet, yards, miles
  }
  enum Time: String, CaseIterable, UnitProtocol {
    case seconds, minutes, hours, days
  }
  enum Volume: String, CaseIterable, UnitProtocol {
    case milliliters, liters, cups, pints, gallons
  }
}

询问用户可以使用此部分转换哪些单元:

struct ContentView: View {
  @State private var units = Units.temperature
...

var body: some View {
    NavigationView {
      Form {
        Section {
          Picker("Units to convert", selection: $units) {
            ForEach(Units.allCases, id: \.self) {
              Text($0.rawValue.capitalized)
            }
          }
        }
      }
    }
  }
}

从单位枚举选择案例后,我想从所选嵌套的枚举中显示.allcase。例如:我选择温度,所以在此之后,我将在foreach上迭代units.temperature.allcases。但是我该怎么做呢? 我试图在状态属性下方创建新变量:

  @State private var units = Units.temperature
  private var unit: UnitProtocol {
    switch units {
    case .temperature:
      return Units.Temperature.Type
    case .time:
      return Units.Time.Type
    case .length:
      return Units.Length.Type
    case .volume:
      return Units.Volume.Type
    }
  }

在身体中,

Section {
          Picker("Convert from", selection: $units) {
            ForEach(unit.allCases, id: \.self) {
              Text($0.rawValue.capitalized)
            }
          }
        }

可悲的是,这对我不起作用。我应该怎么办? 我会为每个答案

P.S.对不起,我的英语不是我的母语

I'm very new to swift and trying to make Unit Conversion App.
I use nested enums for all units available:

protocol UnitProtocol {

}

enum Units: String, CaseIterable, UnitProtocol {
  case temperature, length, time, volume
  enum Temperature: String, CaseIterable, UnitProtocol {
    case celsius, fahrenheit, kelvin
  }
  enum Length: String, CaseIterable, UnitProtocol {
    case meters, kilometers, feet, yards, miles
  }
  enum Time: String, CaseIterable, UnitProtocol {
    case seconds, minutes, hours, days
  }
  enum Volume: String, CaseIterable, UnitProtocol {
    case milliliters, liters, cups, pints, gallons
  }
}

The user is asked what units to convert with this section:

struct ContentView: View {
  @State private var units = Units.temperature
...

var body: some View {
    NavigationView {
      Form {
        Section {
          Picker("Units to convert", selection: $units) {
            ForEach(Units.allCases, id: \.self) {
              Text($0.rawValue.capitalized)
            }
          }
        }
      }
    }
  }
}

After choosing case from Units enum, I want to display .allCases from selected nested Enum. For example: I chose temperature, so after that I'll iterate with forEach on Units.Temperature.allCases. But how can I make this?
I tried to create new variable just below the State property:

  @State private var units = Units.temperature
  private var unit: UnitProtocol {
    switch units {
    case .temperature:
      return Units.Temperature.Type
    case .time:
      return Units.Time.Type
    case .length:
      return Units.Length.Type
    case .volume:
      return Units.Volume.Type
    }
  }

and in the body use this

Section {
          Picker("Convert from", selection: $units) {
            ForEach(unit.allCases, id: \.self) {
              Text($0.rawValue.capitalized)
            }
          }
        }

Sadly, that didn't work for me. What should I do?
I would be very glad for every answer

P.S. Sorry for my English, it's not my native language

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文