在 SwiftUI 中构建列表视图时出错
Getting errors with building a List view in SwiftUI
对于我的应用程序,我试图从元组数组中创建一个列表,但出现以下错误:
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
协议,如果我在你那里。更方便也更干净。
对于我的应用程序,我试图从元组数组中创建一个列表,但出现以下错误:
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
协议,如果我在你那里。更方便也更干净。