BigBoy1337
BigBoy1337

Reputation: 4963

Swift: Cannot convert value of type '() -> ()' to closure result type 'Void'

Heres all the code. there is a button press action function that calls prepareVideo.

@IBAction func nextButtonPressed(_ sender: Any) {

    if MyVariables.isScreenshot == true {
        prepareScreenshot {
            self.moveOn()
        }
    } else {
        prepareVideo()
    }
}  
func prepareVideo(){
        let outputFileName = NSUUID().uuidString
        let outputFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((outputFileName as NSString).appendingPathExtension("mov")!)
        self.videoURL = URL(fileURLWithPath: outputFilePath)
        trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)], completion: prepareVideoThumbnail {
            self.moveOn
        })          //Xcode mentions error here
    }

func trimVideo (sourceURL: URL, destinationURL: URL, trimPoints: TrimPoints, completion: @escaping () -> ()) {

        guard sourceURL.isFileURL else { return }
        guard destinationURL.isFileURL else { return }

        let options = [
            AVURLAssetPreferPreciseDurationAndTimingKey: true
        ]

        let asset = AVURLAsset(url: sourceURL, options: options)
        let preferredPreset = AVAssetExportPresetPassthrough

        if  verifyPresetForAsset(preset: preferredPreset, asset: asset) {

            let composition = AVMutableComposition()
            let videoCompTrack = composition.addMutableTrack(withMediaType: .video, preferredTrackID: CMPersistentTrackID())
            let audioCompTrack = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: CMPersistentTrackID())

            guard let assetVideoTrack: AVAssetTrack = asset.tracks(withMediaType: .video).first else { return }
            guard let assetAudioTrack: AVAssetTrack = asset.tracks(withMediaType: .audio).first else { return }

            var accumulatedTime = kCMTimeZero
            for (startTimeForCurrentSlice, endTimeForCurrentSlice) in trimPoints {
                let durationOfCurrentSlice = CMTimeSubtract(endTimeForCurrentSlice, startTimeForCurrentSlice)
                let timeRangeForCurrentSlice = CMTimeRangeMake(startTimeForCurrentSlice, durationOfCurrentSlice)

                do {
                    try videoCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetVideoTrack, at: accumulatedTime)
                    try audioCompTrack!.insertTimeRange(timeRangeForCurrentSlice, of: assetAudioTrack, at: accumulatedTime)
                    accumulatedTime = CMTimeAdd(accumulatedTime, durationOfCurrentSlice)
                }
                catch let compError {
                    print("TrimVideo: error during composition: \(compError)")
                }
            }

            guard let exportSession = AVAssetExportSession(asset: composition, presetName: preferredPreset) else { return }

            exportSession.outputURL = destinationURL as URL
            exportSession.outputFileType = AVFileType.m4v
            exportSession.shouldOptimizeForNetworkUse = true

            removeFileAtURLIfExists(url: destinationURL as URL)

            exportSession.exportAsynchronously {
                completion()
            }
        }
        else {
            print("TrimVideo - Could not find a suitable export preset for the input video")
        }
    }
func prepareVideoThumbnail(completion: @escaping () -> Void) {
    guard let VideoURL = self.videoURL else { return }
    self.thumbnailImage = setThumbnailFrom(path: VideoURL)
    completion()
    //moveOn()
    //DispatchQueue.main.async {

    //  self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
    //}
}

func moveOn(){
        guard self.thumbnailImage != nil else {
            return
        }
        if MyVariables.isScreenshot == true {
            guard self.screenshotOut != nil else {
                return
            }
            self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
        } else {
            guard self.thumbnailImage != nil else {
                return
            }
            self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
            //now I set those three varibles?
        }
    }

PrepareVideo takes no callbacks/completion handlers but it calls trimVideo which does take a completion handler. In this I call prepareVideoThumbnail which should call its completion handler { self.moveOn }. PrepareVideoThumbnail should be of type () -> () which is what trimVideo suspects. So I'm not sure why its complaining about closure result type 'Void'. I realize that self.moveOn() results in this, but Im not calling it, notice I am using self.moveOn without the parentheses. How do I fix this?

Upvotes: 2

Views: 6536

Answers (1)

rmaddy
rmaddy

Reputation: 318794

The syntax of:

trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)], completion: prepareVideoThumbnail {
    self.moveOn
})

should be:

trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)]) {
    self.prepareVideoThumbnail() {
        self.moveOn()
    }
}

BTW - it makes no sense to have a completion parameter for functions that do not do any asynchronous processing (like your preparreVideoThumbnail function).

Then your code becomes:

trimVideo(sourceURL: self.footageURL!, destinationURL: self.videoURL!, trimPoints: [(trimmerView.startTime!,trimmerView.endTime!)]) {
    self.prepareVideoThumbnail()
    self.moveOn()
}

And eliminate the completion parameter on the prepareVideoThumbnail function.

Upvotes: 1

Related Questions