可选子数组中的 foreach 循环内的 Swift foreach 循环

问题描述 投票:0回答:1

我是 Swift 新手,所以如果我有一些不正确的术语,请原谅我。

我有一个膳食计划的日历视图。父数组生成给定月份的天数,子数组包含当天计划的膳食。当天可能没有计划任何膳食,因此子数组是可选的。

我的问题是如何正确处理这个可选数组?

我尝试过 if 语句来检查数组,但它会抛出错误。

前几天的 JSON:

[
    {
        "id": 1,
        "monthDate": "2024-01-01T00:00:00+0000",
        "recipetitle": [
            {
                "id": 1,
                "rtitle": "Garlic Herb Prawn Salad"
            }
        ]
    },
    {
        "id": 2,
        "monthDate": "2024-01-02T00:00:00+0000"
    },
    {
        "id": 3,
        "monthDate": "2024-01-03T00:00:00+0000"
    },
    {
        "id": 4,
        "monthDate": "2024-01-04T00:00:00+0000",
        "recipetitle": [
            {
                "id": 1,
                "rtitle": "Garlic Herb Prawn Salad"
            }
        ]
    }
]

我有我的结构

struct MonthDates: Identifiable, Decodable, Hashable {
    var id: Int
    var monthDate: String
    var recipetitle: [Recipetitle]?
}

struct Recipetitle: Identifiable, Codable, Hashable {
    var id = Int
    var rtitle: String
}

这是我的部分视图:

ForEach(dataModel.listOfDates, id: \.id) { listItems in
  NavigationLink {
    // Navigate to a new page here
  } label: {
    HStack {
      let sideDate = convertStringToDate(date: listItems.monthDate)
      let abbreviatedDay = abbreviatedDay(date: sideDate)
      let dayNumber = dayNumber(date: sideDate)
      VStack(alignment: .leading) {
        HStack {
          VStack {

            Text(abbreviatedDay)
              .font(.caption)
              .foregroundColor(.gray)
            Text(dayNumber)
              .foregroundColor(.red)
          }
          .frame(width: 40)
          Divider()
            .frame(maxHeight: 80)
          VStack(alignment: .leading) {
            ForEach(listItems.recipetitle, id: \.id) { listItems1 in
              HStack {
                Text(listItems1.rtitle)
                  .font(.system(size: 11))
                Spacer()
                Button {

                } label: {
                  Text("Delete")
                    .font(.system(size: 10))
                    .foregroundColor(Color.red)
                }
                .buttonStyle(.bordered)
                .controlSize(.regular)
              }
              .padding(.bottom, 3)
              Spacer()
            }
          }
        }
        Divider()
      }
      .frame(height: 100)
      .padding(.leading, 5)
      .padding(.trailing, 5)
    }
  }
}
arrays swift swiftui
1个回答
0
投票

您可以在尝试循环

if let
 之前添加 
listItems.recipetitle

if let recipeTitle = listItems.recipletitle {
    /// This will get executed if the list item above has one, or more, recipe titles
}

完整代码如下:

  ForEach(dataModel.listOfDates, id: \.id) { listItems in
  NavigationLink {
    // Navigate to a new page here
  } label: {
    HStack {
      let sideDate = convertStringToDate(date: listItems.monthDate)
      let abbreviatedDay = abbreviatedDay(date: sideDate)
      let dayNumber = dayNumber(date: sideDate)
      VStack(alignment: .leading) {
        HStack {
          VStack {

            Text(abbreviatedDay)
              .font(.caption)
              .foregroundColor(.gray)
            Text(dayNumber)
              .foregroundColor(.red)
          }
          .frame(width: 40)
          Divider()
            .frame(maxHeight: 80)

          if let recipeTitle = listItems.recipleTitle {

             VStack(alignment: .leading) {
            ForEach(listItems.recipetitle, id: \.id) { listItems1 in
              HStack {
                Text(listItems1.rtitle)
                  .font(.system(size: 11))
                Spacer()
                Button {

                } label: {
                  Text("Delete")
                    .font(.system(size: 10))
                    .foregroundColor(Color.red)
                }
                .buttonStyle(.bordered)
                .controlSize(.regular)
              }
              .padding(.bottom, 3)
              Spacer()
            }
          }
          }
          
        }
        Divider()
      }
      .frame(height: 100)
      .padding(.leading, 5)
      .padding(.trailing, 5)
    }
  }
}

您提供的代码对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.