自定义视图有效,但其预览无法编译
A custom View works, but its preview does not compile
对于我在Githuba simple test project准备的问题:
它是一个 SwiftUI 带有自定义视图的列表 TopRow.swift:
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菜鸟帮我看明白
我试过在那里放一个分号,但没有成功。
您不能将这样的命令式代码放在 struct
或 class
的顶层——顶层是为函数和属性的声明保留的。
您可以将 topEntity
改为计算的 属性 - 这样您的命令式赋值就可以进入 { }
而不是存在于顶层:
struct TopRow_Previews: PreviewProvider {
static var topEntity : TopEntity {
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"
return topEntity
}
static var previews: some View {
TopRow(top: topEntity)
.padding()
.previewLayout(.sizeThatFits)
}
}
对于我在Githuba simple test project准备的问题:
它是一个 SwiftUI 带有自定义视图的列表 TopRow.swift:
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菜鸟帮我看明白
我试过在那里放一个分号,但没有成功。
您不能将这样的命令式代码放在 struct
或 class
的顶层——顶层是为函数和属性的声明保留的。
您可以将 topEntity
改为计算的 属性 - 这样您的命令式赋值就可以进入 { }
而不是存在于顶层:
struct TopRow_Previews: PreviewProvider {
static var topEntity : TopEntity {
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"
return topEntity
}
static var previews: some View {
TopRow(top: topEntity)
.padding()
.previewLayout(.sizeThatFits)
}
}