gpt4 book ai didi

swiftui - 无法将 'Binding' 类型的值转换为预期的参数类型 'Binding<_>'

转载 作者:行者123 更新时间:2023-12-03 15:02:27 24 4
gpt4 key购买 nike

我正在尝试创建一个 TabView在 SwiftUI 中使用以下代码:

@State var selection = 0

var body: some View {
TabView(selection: $selection) {
DataGridPage(type: "media").tabItem {
Image(systemName: "photo.on.rectangle")
.imageScale(.large)
.foregroundColor(.yellow)
}
.tag(1)

DataGridPage(type: "files").tabItem {
Image(systemName: "doc.on.doc")
.imageScale(.large)
.foregroundColor(.yellow)
}
.tag(2)
}
}

但我收到错误 Cannot convert value of type 'Binding<Int>' to expected argument type 'Binding<_>' .我看到变量 selection是整数,这是正确的类型,但由于某种原因警告仍然存在。

最佳答案

我解决了这个问题。问题是TabView即使闭包中存在一些错误,也会显示此错误。所以创建TabView的代码是正确的,但问题是我初始化的方式 DataGridPage .我更改了房产名称 typedataDataGridPage但在这里我仍在使用 type属性(property)。我修复了它,它停止向我显示警告。

我认为 SwiftUI 是一个新框架,它在调试方面还有很多改进要做。我希望它将来会成熟,我们将能够指出确切的错误,而不是这种模糊的陈述。

新代码现在看起来像这样:

@State var selection = 0

var body: some View {
TabView(selection: $selection) {
DataGridPage(data: "media").tabItem {
Image(systemName: "photo.on.rectangle")
.imageScale(.large)
.foregroundColor(.yellow)
}
.tag(1)

DataGridPage(data: "files").tabItem {
Image(systemName: "doc.on.doc")
.imageScale(.large)
.foregroundColor(.yellow)
}
.tag(2)
}
}

希望它可以帮助面临类似问题的人。

关于swiftui - 无法将 'Binding<Int>' 类型的值转换为预期的参数类型 'Binding<_>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58724301/

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