【问题标题】:Cannot convert value of type '(Int) -> VStack<Button<Text>>' to expected argument type '(_) -> _'无法将类型 '(Int) -> VStack<Button<Text>>' 的值转换为预期的参数类型 '(_) -> _'
【发布时间】:2020-04-11 04:26:53
【问题描述】:

我的错误消息是:无法将“无法将类型'(Int) -> VStack>'的值转换为预期的参数类型'(_) -> _''类型的值

我知道代码的错误位是我在调用 getMedia 时使用 'array[index].documentID' 但我不知道为什么或如何解决。

你能帮忙吗?

这是我的视图代码:

struct weekDataList: View {
let weekDataF = weekDataFunc()
@ObservedObject var MainViews = MainView()
var body: some View {
    VStack {
        ForEach(array.count, id: \.self) { index in
            VStack{
                Button(action: {MainViews.getMedia(documentId: array[index].documentID)}) 
                               {Text(array[index].startDate + "-" + array[index].endDate)}
            }
        }.position(x:100,y:-100)
    }
}

【问题讨论】:

  • MainViews.getMedia 返回 View,但不允许在 action block 中使用 SwiftUI View。你能描述一下你想要达到的目标吗?
  • 我没有从 MainViews.getMedia 返回任何内容。它只接受一个参数,我想在单击按钮时运行一些功能
  • 那你会显示MainView 代码吗?或者提供最小可测试的独立演示,因为提供的快照有许多依赖的不可用组件,它们自己会产生很多编译器错误。
  • 现在,这就是 MainView 的样子。不多,我只是在测试如何让按钮在点击时运行一些不同的功能:class MainView: ObservableObject{ @Published var updateView: Bool = false func getMedia(documentId: Int) { print("test") } func chunkVideos() { videosChunked = videos.chunked(into:3) } }

标签: swiftui


【解决方案1】:

仅通过代码阅读,因为正如我评论的那样,快照不可编译

ForEach(0..<array.count) { index in
        ^^^^^^^^^^^^^^^ should be range and no id for indexes

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2017-01-09
    • 2017-02-08
    • 2018-10-01
    • 2021-12-31
    相关资源
    最近更新 更多