gpt4 book ai didi

Swift:无法将类型 '() -> ()' 的值转换为闭包结果类型 'Void'

转载 作者:行者123 更新时间:2023-11-28 05:59:50 25 4
gpt4 key购买 nike

这是所有的代码。有一个调用 prepareVideo 的按钮按下 Action 函数。

@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 不接受回调/完成处理程序,但它调用 trimVideo,它确实接受完成处理程序。在这里我调用 prepareVideoThumbnail 应该调用它的完成处理程序 { self 移动 }. PrepareVideoThumbnail 应该是 trimVideo 怀疑的类型 () -> ()。所以我不确定为什么它会提示关闭结果类型“Void”。我意识到 self.moveOn() 导致了这个,但我没有调用它,请注意我使用的是不带括号的 self.moveOn。我该如何解决?

最佳答案

语法:

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

应该是:

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

顺便说一句 - 对于不执行任何异步处理的函数(例如您的 preparreVideoThumbnail 函数)设置完成参数是没有意义的。

然后你的代码变成:

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

并消除 prepareVideoThumbnail 函数的完成参数。

关于Swift:无法将类型 '() -> ()' 的值转换为闭包结果类型 'Void',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50144179/

25 4 0
文章推荐: ios - 调度组错误
文章推荐: arrays - 将 Array 作为参数传递给函数时出现“[String].Type”错误
文章推荐: html - 水平下拉菜单的子菜单不垂直
文章推荐: swift - 无法将 Realm 列表从 List 转换为 List