如何从切换嵌套枚举中返回一些枚举?
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论