我正在关注 Apple 的 WWDC2020 SwiftUI 视频“SwiftUI 中的新功能”
这个:https ://developer.apple.com/videos/play/wwdc2020/10041/
我已经在提供演示代码的 Apple 的“Apple Developer”应用程序中打开了这个视频。我刚刚将这个原始代码复制到 Xcode 中,但它不能正常工作(下面的截图),为什么?
原始代码:
struct ContentView: View {
var items: [Item]
var body: some View {
ScrollView {
LazyVGrid(columns: Array(repeating: GridItem(), count: 4)]) {
ForEach(items) { item in
ItemView(item: item)
}
}
.padding()
}
}
}
struct Item: Identifiable {
var name: String
var id = UUID()
var icon: Image {
Image(systemName: name)
}
var color: Color {
colors[colorIndex % (colors.count - 1)]
}
private static var nextColorIndex: Int = 0
private var colorIndex: Int
init(name: String) {
self.name = name
colorIndex = Self.nextColorIndex
Self.nextColorIndex += 1
}
}
struct ItemView: View {
var item: Item
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 8, style: .continuous)
.fill()
.layoutPriority(1)
.foregroundColor(item.color)
item.icon
.resizable()
.aspectRatio(contentMode: .fit)
.padding(.all, 16)
.foregroundColor(.white)
}
.frame(width: 176, height: 110)
}
}