问题
I am trying to take/record video asynchronously on the main thread. However, when I call dispatch.main.async
, I always get the error:
use of unresolved identifier DispatchQueue
I've looked everywhere from WWDC to Apple's Documentation, but I see no evidence of the type being deprecated.
Here is the code:
if !self.cameraEngine.isRecording {
if let url = CameraEngineFileManager.temporaryPath("video.mp4") {
self.cameraButton.setTitle("stop recording", forState: [])
self.cameraEngine.startRecordingVideo(url, blockCompletion: { (url: NSURL?, error: NSError?) -> (Void) in
if let url = url {
DispatchQueue.main.async {
self.cameraButton.setTitle("start recording", for: .normal)
CameraEngineFileManager.saveVideo(url, blockCompletion: { (success: Bool, error: Error?) -> (Void) in
if success {
let alertController = UIAlertController(title: "Success, video saved !", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alertController, animated: true, completion: nil)
}
})
}
}
})
}
}
else {
self.cameraEngine.stopRecordingVideo()
}
}
回答1:
You can either put
import Foundation
or
import Dispatch
at the beginning of your code to start using DispatchQueue class without any issue.
回答2:
The giveaway here is this line:
self.cameraButton.setTitle("stop recording", forState: [])
That is not Swift 3. Therefore your file is a Swift 2.3 file. Therefore there is no such thing as DispatchQueue. You should be using
dispatch_async(dispatch_get_main_queue()) { ...
回答3:
I do think that you don't have one of these in your frameworks list. Try to import one of these and try putting DispatchQueue's in your file.
import Foundation
import UIKit
// Any System Framework
This can happen if your file is not a Swift file, or your file is a Swift file but you don't have any frameworks on the top.
来源:https://stackoverflow.com/questions/39679260/dispatchqueue-in-swift-3-appears-as-unresolved-identifier