[英]Yellow warning: Conditional cast from 'AVCaptureVideoPreviewLayer' to 'AVCaptureVideoPreviewLayer' always succeeds
I have this bit of code and it's throwing a yellow warning.我有这段代码,它发出黄色警告。 I can't work out how to code it so the yellow warning goes away.我不知道如何编码,所以黄色警告消失了。 Trying to clean up my code after converting from Swift-2 -> 3 -> 4.从 Swift-2 -> 3 -> 4 转换后尝试清理我的代码。
if let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) as? AVCaptureVideoPreviewLayer {
previewLayer.bounds = imageView.bounds
previewLayer.position = CGPoint(x: 140, y: 140)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
imageView.layer.addSublayer(previewLayer)
view.addSubview(imageView)
}
Removing as? AVCaptureVideoPreviewLayer
删除as? AVCaptureVideoPreviewLayer
as? AVCaptureVideoPreviewLayer
shows an error: as? AVCaptureVideoPreviewLayer
显示错误:
Initializer for conditional binding must have Optional type, not 'AVCaptureVideoPreviewLayer'条件绑定的初始化程序必须具有 Optional 类型,而不是“AVCaptureVideoPreviewLayer”
AVCaptureVideoPreviewLayer(session: captureSession)
always returns an object of type AVCaptureVideoPreviewLayer
, not of type AVCaptureVideoPreviewLayer?
AVCaptureVideoPreviewLayer(session: captureSession)
总是返回一个AVCaptureVideoPreviewLayer
类型的对象,而不是AVCaptureVideoPreviewLayer?
类型的对象AVCaptureVideoPreviewLayer?
(ie the returned object is not an optional and so it can't be nil). (即返回的对象不是可选的,因此它不能为零)。
When you do if let
, Swift expects an Optional type.当你执行if let
,Swift 需要一个 Optional 类型。 Since no optional type is created, the error occurs.由于没有创建可选类型,因此发生错误。
My suggestion is to remove the if
part and just have我的建议是删除if
部分并只拥有
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer.bounds = imageView.bounds
previewLayer.position = CGPoint(x: 140, y: 140)
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
imageView.layer.addSublayer(previewLayer)
view.addSubview(imageView)
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.