0

我试过关注thisthisthis,但是我在从字符串数组中获取数据时遇到了问题。

我的状态设置为

 businessHours = : [
    "Monday: 7:00 AM – 7:00 PM",
    "Tuesday: 7:00 AM – 7:00 PM",
    "Wednesday: 7:00 AM – 7:00 PM",
    "Thursday: 7:00 AM – 7:00 PM",
    "Friday: 7:00 AM – 7:00 PM",
    "Saturday: 7:00 AM – 7:00 PM",
    "Sunday: Closed"
 ]

这只会让我得到数组中的第一个元素

ForEach(businessHours.indices) {
    Text(self.businessHours[$0])
}

这失败了

ForEach(businessHours.indices) {
    Text(self.businessHours)
}

这在控制台中有效

for businesshour in businessHours {
print("Hours: \(businesshour).")
}

我在这里做错了什么?

4

2 回答 2

1

这是一个有效的演示。使用 Xcode 12 / iOS 14 测试

在此处输入图像描述

struct DemoView: View {
    let businessHours = [
         "Monday: 7:00 AM – 7:00 PM",
         "Tuesday: 7:00 AM – 7:00 PM",
         "Wednesday: 7:00 AM – 7:00 PM",
         "Thursday: 7:00 AM – 7:00 PM",
         "Friday: 7:00 AM – 7:00 PM",
         "Saturday: 7:00 AM – 7:00 PM",
         "Sunday: Closed"
     ]

    var body: some View {
        VStack {
            ForEach(businessHours.indices) {
                 Text(self.businessHours[$0])
            }
        }
    }
}
于 2020-10-28T17:31:06.380 回答
0

我想到了。

(0..<businessHours.count, id: \.self) { index in
       Text(self.businessHours[index])
}
于 2020-10-28T19:20:06.000 回答