ios - 返回文本或按钮的Swiftui函数?
问题描述
我有一个简单的函数来返回Text
or Button
,但我收到以下错误:Function declares an opaque return type, but the return statements in its body do not have matching underlying types
这是我的功能:
private func cellContent(for cell: Cell) -> some View {
if (cell.editable) {
return Button(action: { print("hi") }) { Text(cell.content) }
}
return Text(cell.content)
}
我该如何解决这个问题或以不同的方式做到这一点?
谢谢
解决方案
这是正确的变体。
注意:ViewBuilder
显式使用时禁用return
,因此使用时必须删除return
(s),否则会再次出现编译器错误。
使用 Xcode 12 / iOS 14 测试
@ViewBuilder
private func cellContent(for cell: Cell) -> some View {
if (cell.editable) {
Button(action: { print("hi") }) { Text(cell.content) }
}
Text(cell.content)
}
推荐阅读
- kubernetes - Kubeflow - 无法加载仪表板菜单链接
- css - 有什么方法可以强制成帧器运动使用宽度而不是变换?
- python - 使用健身房创建新的多智能体环境
- ajax - c#Net Core方法执行多次-创建多个线程
- javascript - 将日期设置为文本的 html 表导出到 Excel
- r - 如何找到 R 中哪些预测变量对高度相关?给定 3 个预测变量 X1、X2、X3,因变量为 Y
- machine-learning - xgboost 中使用什么类型的决策树?
- r - 如何创建一个函数来显示 table1 包中的第 25 和第 75 个百分位数 (IQR)
- python - 改变变量不影响结果
- java - 在线编译器从 System.in 获取输入时导致崩溃