gpt4 book ai didi

swift - TextField ViewModifier 不符合 ViewModifier?

转载 作者:行者123 更新时间:2023-12-02 15:54:48 25 4
gpt4 key购买 nike

我的一个 View 中有这段代码:

struct TextFieldClearButton: ViewModifier {

@Binding var text: String

func body(content: Content) -> some View {

HStack {

content

if !text.isEmpty {
Button(
action: { self.text = "" },
label: {
Image(systemName: "delete.left")
.foregroundColor(Color(UIColor.opaqueSeparator))
}
)
}
}
}
}

我得到两个错误:

  1. 类型“TextFieldClearButton”不符合协议(protocol)“ViewModifier”
  2. 静态方法“buildBlock”要求“Content”符合“View”

我怎样才能摆脱这些错误并使这个修改器编译?

enter image description here

看来我根本无法使用ViewModifier。也添加 super 简单的案例错误??:

enter image description here

最佳答案

您可能有一个 struct/class在您名为 Content 的项目中

如果您有 Xcode 的标准深色主题,“mint”/“greenish”表示它是“Project”定义的。

enter image description here

当您使用 Apple 的定义时,它呈粉红色/紫色,如 ViewModifier , View , 和 String在你的屏幕截图中。

enter image description here

搜索 struct Content , class Content , enum Content等。在您的项目中,您会找到副本,然后只需更改副本的名称即可。

它也可以是一个通用的 <Content: SomeProtocol><Content>typealias Content

您可以通过更具体的方式确认重复项

enter image description here

关于swift - TextField ViewModifier 不符合 ViewModifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71698728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com