简体   繁体   English

使用 if 语句时:类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议

[英]While using an if statement: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols

I am trying to implement a button that activates a ForEach loop to go through each item in the list and mark it as delayed.我正在尝试实现一个按钮,该按钮通过列表中的每个项目激活 ForEach 循环到 go 并将其标记为延迟。 This is my code:这是我的代码:

import SwiftUI

struct AllJobsDelayWeather: View{
    var jobIndividualViewModel: JobIndividualViewModel
    @State public var comparisonDate = getCurrentDate()
    
    
    var body: some View{
        let jobDate = ParserDate(date: jobIndividualViewModel.job.date)
        Text(" ")
        if jobIndividualViewModel.job.completed == false && jobDate == comparisonDate{ //error is here
            jobIndividualViewModel.delayWeather(jobIndividualViewModel.job) 
               Text(" ")
            }
        Text(" ")
        
        }
}

This is in the "main" (if you can call it) portion of the program这是程序的“主要”(如果你可以调用它)部分

Button("Delay Bad Weather"){
                    ForEach(jobViewModel.jobIndividualViewModels) { jobVM in
                        AllJobsDelayWeather(jobIndividualViewModel: jobVM)
                    }
                }.padding()

I am getting this error: Type '()' cannot conform to 'View';我收到此错误:类型“()”不能符合“视图”; only struct/enum/class types can conform to protocols.只有结构/枚举/类类型可以符合协议。 The function works and has been "ad hoc" tested. function 工作正常并且已经过“临时”测试。 I don't know how to get rid of the error that the compiler is throwing.我不知道如何摆脱编译器抛出的错误。

EDIT Currently trying to do this:编辑目前正在尝试这样做:

//in body
Button("Delay Bad Weather"){
                    ForEach(jobViewModel.jobIndividualViewModels) { jobVM 
//errors getting thrown above
                        AllJobDelayWeather(jobVM: jobVM)
                    }
                }.padding()

//outside if struct in the same file

func AllJobDelayWeather(jobVM: JobIndividualViewModel){
    let comparisonDate = getCurrentDate()
    
    let jobDate = ParserDate(date: jobVM.job.date)
    if jobVM.job.completed == false && jobDate == comparisonDate{
        jobVM.delayWeather(jobVM.job)

        }
    
}

I am now getting 2 instances of the error, one on the F of ForEach and the { before the first instance of jobVM.我现在收到 2 个错误实例,一个在 ForEach 的 F 上,一个在 jobVM 的第一个实例之前的 {。

As mentioned in the comments, you're using ForEach , which is a SwiftUI view, inside a Button's action closure, where you can't have views.正如评论中提到的,您正在使用ForEach ,这是一个 SwiftUI 视图,位于 Button 的action闭包内,您无法在其中查看。 Looking at your updated code, it looks like you just want a regular forEach.查看更新后的代码,您似乎只需要一个常规的 forEach。 Something like:就像是:

Button("Delay Bad Weather") {
   jobViewModel.jobIndividualViewModels.forEach { jobVM
     allJobDelayWeather(jobVM: jobVM)
   }
}.padding()
func allJobDelayWeather(jobVM: JobIndividualViewModel){
    let comparisonDate = getCurrentDate()
    
    let jobDate = ParserDate(date: jobVM.job.date)
    if jobVM.job.completed == false && jobDate == comparisonDate{
        jobVM.delayWeather(jobVM.job)
    }
}

I changed your function name to lowercase because that's the preferred naming convention in Swift.我将您的 function 名称更改为小写,因为这是 Swift 中的首选命名约定。 It's also somewhat unusual to have a function floating outside of a view like you imply in your comments -- it probably belongs inside the view or inside the view model (hard to say since I can't see all of your code).就像您在评论中暗示的那样,让 function 漂浮在视图之外也有些不寻常——它可能属于视图内部或视图 model 内部(很难说,因为我看不到您的所有代码)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 请帮忙:类型'()'不能符合'View'; 只有结构/枚举/类类型可以符合协议 - Please help: Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 如何修复类型“()”不能符合“视图”; 只有结构/枚举/类类型可以符合协议 - how do i fix Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols type() 不能符合 View; 只有结构/枚举/类类型可以符合协议 - Type () cannot conform to View; only struct/enum/class types can conform to protocols 类型 '() -> ()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '() -> ()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols 类型“MovieSearchContainer.Type”不能符合“Decodable”; 只有结构/枚举/类类型可以符合协议 - Type 'MovieSearchContainer.Type' cannot conform to 'Decodable'; only struct/enum/class types can conform to protocols 协议类型'*'的值不能符合'*'; 只有结构/枚举/类类型可以符合协议 - Value of protocol type '*' cannot conform to '*'; only struct/enum/class types can conform to protocols Swift 协议类型“XXX”的值不能符合“可识别”; 只有结构/枚举/类类型可以符合协议 - Swift Value of protocol type 'XXX' cannot conform to 'Identifiable'; only struct/enum/class types can conform to protocols 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM