SwiftUI SceneDelegate - 调用中参数 'index' 的 contentView 缺少参数

SwiftUI SceneDelegate - contentView Missing argument for parameter 'index' in call

我正在尝试使用数据数组的 ForEach 和 NavigationLink 创建列表。

我相信我的代码(见 post 的末尾)是正确的,但由于以下原因,我的构建失败了 “调用中缺少参数 'index' 的参数”并将我带到 SceneDelegate.swift 一个我以前不必冒险的地方。

// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()

如果我修改为运行,我可以获得代码;

let contentView = ContentView(habits: HabitsList(), index: 1)

但是我所有的链接都包含相同的数据,这是有道理的,因为我正在命名索引位置。

我已经尝试过,索引:self.index(这是我在 NavigationLink 中使用的)并收到不同的错误消息 - 无法将类型“(Any) -> Int”的值转换为预期参数输入 'Int'

以下是我的代码片段供参考;

struct HabitItem: Identifiable, Codable {
    let id = UUID()
    let name: String
    let description: String
    let amount: Int
}

class HabitsList: ObservableObject {
    @Published var items = [HabitItem]()
}

struct ContentView: View {
    @ObservedObject var habits = HabitsList()
    @State private var showingAddHabit = false
    var index: Int
        
    var body: some View {
        NavigationView {
            List {
                ForEach(habits.items) { item in
                    NavigationLink(destination: HabitDetail(habits: self.habits, index: self.index)) {
                        HStack {
                            VStack(alignment: .leading) {
                                Text(item.name)
                                    .font(.headline)
                                Text(item.description)
                            }
                        }
                    }
                }
            }
        }
    }
}

struct HabitDetail: View {
    @Environment(\.presentationMode) var presentationMode
    @ObservedObject var habits: HabitsList

    var index: Int
    
    var body: some View {
        NavigationView {
            Form {
                Text(self.habits.items[index].name)
            }
        }
    }
}

您可能不需要将整个 ObservedObject 传递给 HabitDetail

只传递一个 HabitItem 就足够了:

struct HabitDetail: View {
    @Environment(\.presentationMode) var presentationMode
    let item: HabitItem

    var body: some View {
        // remove `NavigationView` form the detail view
        Form {
            Text(item.name)
        }
    }
}

然后你可以修改你的ContentView:

struct ContentView: View {
    @ObservedObject var habits = HabitsList()
    @State private var showingAddHabit = false

    var body: some View {
        NavigationView {
            List {
                // for every item in habits create a `linkView`
                ForEach(habits.items, id:\.id) { item in
                    self.linkView(item: item)
                }
            }
        }
    }
    
    // extract to another function for clarity
    func linkView(item: HabitItem) -> some View {
        // pass just a `HabitItem` to the `HabitDetail`
        NavigationLink(destination: HabitDetail(item: item)) {
            HStack {
                VStack(alignment: .leading) {
                    Text(item.name)
                        .font(.headline)
                    Text(item.description)
                }
            }
        }
    }
}