Shouldn't the Content in ForEach conform to View

Viewed 78

Shouldn't the Content in ForEach conform to View ?

Here ForEach is defined as

enter image description here

Now I was really certain that it should be declared as:

struct ForEach<Data, ID, Content> where Data: RandomAccessCollection, ID: Hashable,    Content: View

So I kept searching and found this:

enter image description here

So now my question is:

Can Generics be defined somewhere else in the pipeline or is this a mistake in the documentation

2 Answers

Here is an extension that makes generic ForEach is-a View with content as views

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension ForEach : View where Content : View {

    /// The type of view representing the body of this view.
    ///
    /// When you create a custom view, Swift infers this type from your
    /// implementation of the required `body` property.
    public typealias Body = Never
}

There is no mistake, both are correct, when you want use ForEach you have to take decision that you are going use Data or Range! If you want use Data, then you have to provide ID that passed Hashable protocol, but when you decide that you better use Range, in this case you have use Int or \.self

Also in both case you have to pass View/some View as Content to your ForEach.

and you can use both world together with enumerated method.

Related