ios - 使用 IF 查询在 Xcode 中返回错误
问题描述
当我尝试在视图中的 Xcode(Swift 和 SwiftUI)中设置 If 查询时,我收到以下错误:无法使用类型为“(双)”的参数列表调用“填充”。
一旦我注释掉 If 语句,代码就可以正常工作。在下面的代码中,我注释掉了 If 语句。
我该怎么做才能避免错误的输出?错误在 .padding(0.0) 处输出
var body: some View {
NavigationLink(destination: TransactionDetail(product: product)) {
HStack {
Text("X")
.font(.largeTitle)
.clipShape(Circle())
.foregroundColor(.green)
.overlay(Circle().stroke(Color.green, lineWidth: 1))
.shadow(radius: 5)
VStack(alignment: .leading) {
HStack {
Text(product.name)
.font(.headline)
.foregroundColor(.black)
}
Text("21. Januar")
.font(.caption)
.foregroundColor(.black)
}
Spacer()
Text("\(product.price),00 €")
.font(.caption)
.padding(2)
/*
if product.price > 0 {
.foregroundColor(.black)
} else {
.foregroundColor(.black)
}
*/
.cornerRadius(5)
}
.padding(0.0)
}
}
解决方案
这个问题与 SwiftUI 无关。问题是 if 语句没有值。在你的例子中,
if product.price > 0 {
.foregroundColor(.black)
} else {
.foregroundColor(.black)
}
不会评估可应用于Text
视图的方法调用。
这是一个也不会编译的简单示例:
var uc = true
let string = "Hello World"
if uc {
.uppercased()
} else {
.lowercased()
}
在您的情况下,最简单的解决方案是
Text("\(product.price),00 €")
.font(.caption)
.padding(2)
.foregroundColor(product.price > 0 ? .red : .green)
.cornerRadius(5)
使用条件表达式作为前景色的参数。
推荐阅读
- javascript - Javascript:在启动用户触发操作之前等待 AJAX 调用完成
- react-native - 如何在mac os终端上使用放大添加通知命令添加添加(p12)证书的路径?
- javascript - 多个内联插入到表中
- opencv - 长焦镜头对大图像的相机标定误差较大
- laravel-5 - 模型创建后数据未真实保存在数据库中
- python - 我的 app.py 无法在后端接收请求并收到 404 错误
- javascript - 使用 react-apollo 进行服务器端渲染
- gradle - Beam 管道在直接运行器上运行正常时未在 Google Dataflow 中移动
- mysql - SQL 年龄组和唯一编号
- reactjs - 添加钩子组件时,比上一次渲染时渲染的钩子更多