首页 > 解决方案 > 返回文本或按钮的Swiftui函数?

问题描述

我有一个简单的函数来返回Textor 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)
}

我该如何解决这个问题或以不同的方式做到这一点?

谢谢

标签: iosswiftswiftui

解决方案


这是正确的变体。

注意: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)
}

推荐阅读