繁体   English   中英

在SwiftUI中建立清单检视时发生错误

[英]Getting errors with building a List view in SwiftUI

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

无法使用参数类型为'([[EggDayList.EggDay],@escaping([CellDayRow.EggDay])-> NavigationLink)的参数列表调用类型'List <_,_>'的初始化程序。

我之前已经列出了几个清单,但是我看不出这里出了什么问题。

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,并根据此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创建一个struct ,并使其符合可Identifiable协议(如果我在您那里的话)。 更方便,更清洁。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM