对于这个问题,我在 Github准备了一个简单的测试项目:
它是一个带有自定义 View TopRow.swift的 SwiftUI 列表:
struct TopRow: View {
let top:TopEntity
var body: some View {
HStack {
Circle()
.frame(width: 60, height: 60)
Spacer()
Text(top.given ?? "Unknown Person")
.frame(minWidth: 60, maxWidth: .infinity, alignment: .leading)
Spacer()
VStack {
Text("Elo rating: \(top.elo)")
Text("Average time: \(top.avg_time ?? "")")
Text("Average score: \(String(top.avg_score))")
}.fixedSize(horizontal: true, vertical: false)
}.font(.footnote)
}
}
正如您在上面的屏幕截图中看到的那样,它工作正常。
但是它的预览不起作用:
struct TopRow_Previews: PreviewProvider {
static var topEntity = TopEntity(context: PersistenceController.preview.container.viewContext)
topEntity.uid = 19265
topEntity.elo = 2659
topEntity.given = "Alex"
topEntity.motto = "TODO"
topEntity.photo = "https://slova.de/words/images/female_happy.png"
topEntity.avg_score = 18.8
topEntity.avg_time = "03:06"
static var previews: some View {
TopRow(top: topEntity)
.padding()
.previewLayout(.sizeThatFits)
}
}
Xcode 报告语法错误Consecutive declarations on a line must be separated by ';'
那里发生了什么,请帮助我(一个 Swift 新手)理解。
我试图在那里放一个分号,但它并没有真正起作用。