Problem with an enum

Hi,

I haven't been doing Swift for years, but being old (I started designing computer systems in 1967) and housebound its thought of a simple flying app.I'm deeply impressed with SwiftUI - its a revolution. Anyway I'm having trouble with compiling an enum through a ForEach. Whilst its similar to the one I saw in AvocadoToast, I cannot get it to compile.

I have a model file:
import protocol SwiftWebUI.Identifiable

struct TimePeriod
{
var turning = false
var interval = Interval.eight
}

enum Interval: CaseIterable, Hashable, Identifiable
{
case six, eight, ten, twelve
var name: String { return "(self)".capitalized }
}
And the view file:
struct ContentView: View
{
@State var timePeriod: TimePeriod
var body: some View
{
Form
{
Section(header: Text("Time period").font(.title))
{
Picker(selection: $timePeriod.interval, label: Text("Seconds"))
{
ForEach(Interval.allCases)//error:Cannot convert value of type '[Interval]' to expected argument type 'Range'
{
interval in Text(interval.name).tag(interval) //error: Value of type 'Int' has no member 'name'
}
}
}

	 Section(header: Text("Turn indicator").font(.title))
		{
		Text("Angle")
		}
	}

ForEach has a few constructors:

  • init(Data, content: (Data.Element) -> Content)

    Available when

    • ID is Data.Element.ID
    • Content conforms to View
    • Data.Element conforms to Identifiable.
  • init(Range<Int>, content: (Int) -> Content)
    Available when

    • Data is Range<Int>
    • ID is Int
    • Content conforms to View
  • init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)

    Available when

    • Content conforms to View.

Your call looks like this

ForEach(something) {
  some code
}

which can only match the first two initializer. The problem is something is an array of Interval, which is not Range, so it won't match the second one. It can't match the first one either as it requires that Data.Element (in this case, Interval) conforms to protocol Identifiable, which it does not.

You could try to match the third case by adding id argument, like this:

ForEach(interval.allCases, id: \.self) {
  ...
}

PS

You can put codes inside tick marks.

```
like this
```

to turn it into a nice block

like this

Brilliant Lantua! Compiles a treat now! Thank you thank you!