Home > Blockchain >  Cannot convert value of type '()' to expected argument type '(() -> Void)?' i
Cannot convert value of type '()' to expected argument type '(() -> Void)?' i

Time:12-20

I am getting error Cannot convert value of type '()' to expected argument type '(() -> Void)?' while using perform action onAppear function. Below is the code.

This is the view that will display images/Video/PDF files

struct AssetView: View {
    @ObservedObject var messageAttachmentViewModel: MessageAttachmentViewModel
    var index: Int
    var body: some View {
        let messageAttachmentModel = messageAttachmentViewModel.commonMessageAttachmentModel[index]
        switch messageAttachmentModel.uploadStatus {
        case .idle:
            Color.clear.onAppear(perform: messageAttachmentViewModel.uploadFileData(index: index)) // ERROR
        case .failed(let error):
            ProgressView()
        case .loading(let progress):
         //   print(progress)
            ProgressView(value: progress.fractionCompleted, total: Double(progress.totalUnitCount))
        case .loaded:
            ZStack(alignment: .trailing) {
                if messageAttachmentModel.assetType == .Photo {
                    PhotoView(messageAttachment: messageAttachmentModel)
                } else if messageAttachmentModel.assetType == .Video {
                    VideoView(messageAttachment: messageAttachmentModel)
                } else if messageAttachmentModel.assetType == .PDF {
                    PDFView(messageAttachment: messageAttachmentModel)
                }
            }
            .background(Color.red)
            .frame(width: 150, height: 150)
        }
    }
}

CodePudding user response:

Put it into explicit closure, like

case .idle:
    Color.clear.onAppear {
       messageAttachmentViewModel.uploadFileData(index: index)
    }

It reported error because tries cast returned value of messageAttachmentViewModel.uploadFileData() which is Void to expected no-argument closure.

  • Related