SwiftUI: Error while trying to initialise ForEach

Viewed 13

I was trying to work with ForEach when I had the problem that my App crashed every time I was trying to delete Items from a list, but when I reopened the App, the changes were still there. So I found the solution (https://www.hackingwithswift.com/forums/swiftui/deleting-an-item-from-an-array-cause-an-index-out-of-range-in-view/7124) where there was said that I have to initialise my ForEach. I tried

init(_ data: Data, content: @escaping (Data.Element) -> Content)

but I rapidly found out that this cannot work. So I jumped to the definition and I found

extension ForEach where Data == Range<Int>, ID == Int, Content : View {

    /// Creates an instance that computes views on demand over a given constant
    /// range.
    ///
    /// The instance only reads the initial value of the provided `data` and
    /// doesn't need to identify views across updates. To compute views on
    /// demand over a dynamic range, use ``ForEach/init(_:id:content:)``.
    ///
    /// - Parameters:
    ///   - data: A constant range.
    ///   - content: The view builder that creates views dynamically.
    public init(_ data: Range<Int>, @ViewBuilder content: @escaping (Int) -> Content)
}

So I copied the entire code and pasted it to an new SwiftUI View file and changed

public init(_ data: Range<Int>, @ViewBuilder content: @escaping (Int) -> Content)

into

public init(_ data: Data, @ViewBuilder content: @escaping (Data.Element) -> Content)

But now I have a new issue: XCode gives me the error "Initializer requires a body". For a beginner like me, it is very difficult to understand and find out what I have to write as body. Thank you for you help and I appreciate every answer.

0 Answers
Related