【问题标题】:SwiftUI SceneDelegate - contentView Missing argument for parameter 'index' in callSwiftUI SceneDelegate - contentView 在调用中缺少参数“索引”的参数
【发布时间】:2020-07-09 22:15:35
【问题描述】:

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

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

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

如果我修改,我可以让代码运行;

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

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

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

以下是我的sn-ps代码供参考;

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)
            }
        }
    }
}

【问题讨论】:

    标签: swift swiftui swiftui-list swiftui-navigationlink


    【解决方案1】:

    您可能不需要将整个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)
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-08
      • 2020-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2017-04-25
      • 1970-01-01
      相关资源
      最近更新 更多