gpt4 book ai didi

ios - 在 SwiftUI 中构建 ListView 时出错

转载 作者:搜寻专家 更新时间:2023-11-01 06:50:34 24 4
gpt4 key购买 nike

对于我的应用程序,我试图从元组数组中创建一个列表,但出现以下错误:

Cannot invoke initializer for type 'List<_, _>' with an argument list of type '([EggDayList.EggDay], @escaping ([CellDayRow.EggDay]) -> NavigationLink).

我之前列出了几个列表,但我看不出这里出了什么问题。

struct EggDayList: View {

typealias EggDay = (day: Int, mW: Double, measured: Bool, daily: Double)
var actualWeights : [EggDay] = [
(1, 35.48, true, 0.00), (2, 35.23, true, 0.00), (3, 34.92, true, 0.00), (4, 34.64, true, 0.00),(5, 34.29, true, 0.00), (6, 33.86, true, 0.00), (7, 33.48, true, 0.00), (8, 33.12, true, 0.00), (9, 32.77, true, 0.00), (10, 32.45, true, 0.00), (11, 32.08, true, 0.00), (12, 31.87, true, 0.00), (13, 31.55, true, 0.00), (14, 31.46, true, 0.00), (15, 31.33, true, 0.00), (16, 31.24, true, 0.00), (17, 31.14, true, 0.00), (18, 31.02, true, 0.00), (19, 30.90, true, 0.00), (20, 30.81, true, 0.00), (21, 30.65, true, 0.00), (22, 30.54, true, 0.00), (23, 30.31, true, 0.00), (24, 30.12, true, 0.00), (25, 29.97, true, 0.00), (26, 29.82, true, 0.00) ]

var body: some View {

NavigationView {

List(actualWeights) { eggDay in
NavigationLink(destination: EggDetail()) {
CellDayRow(eggDay: eggDay)
}
}
.navigationBarTitle("Measurements")
}
}
}

struct CellDayRow: View {
typealias EggDay = (day: Int, mW: Double, measured: Bool, daily: Double)
let eggDay : [EggDay]
var body: some View {
HStack {
Text("String(eggDay.day)")
.bold()
.font(.headline)
}
}
}

最佳答案

该错误是因为 actualWeights 不是 Identifiable 并且不能单独用作 list 参数。您应该为每个元素指定标识符:

Xcode 11 beta 5 及以上版本:

List(actualWeights, id: \.day) { eggDay in Text("Text") }

Xcode 11 beta 4 及以下版本:

List(actualWeights.identified(by: \.day)) { eggDay in Text("Text") }

但是您应该为每个元素设置一个唯一的 ID,并以此为基础创建列表。像这样:

typealias EggDay = (id: String, day: Int, mW: Double, measured: Bool, daily: Double)
let uniqueEggDay = (UUID().uuidString, 1, 35.48, true, 0.00)
List([uniqueEggDay], id: \.id) { eggDay in Text("Text") }

我会为 EggDay 创建一个 struct 而不是 tuple 并使其符合 Identifiable 协议(protocol),如果我在你那里。它更方便,也更干净。

关于ios - 在 SwiftUI 中构建 ListView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57641235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com