@IBAction func button(sender : AnyObject) {
var videoConnection : AVCaptureConnection!
videoConnection = nil
var connection : AVCaptureConnection
var port : AVCaptureInputPort
var stillImageOutput : AVCaptureStillImageOutput?
for connection in stillImageOutput?.connections{ //this line is where the error is
}
}
我正在尝试使用我的自定义相机拍照,但收到此错误
stillImageOutput
是可选的 - 即使您使用可选链接,也不能在 for
循环中使用,因为如果 stillImageOutput
为 nil,则语句为:
for connection in nil {
}
这没有意义。
要修复它,您必须使用可选绑定:
if let connections = x?.connections {
for connection in connections {
}
}
只是补充Antonio的答案,如果你100%确定在代码的特定部分你的可选不是nil
,你可以直接在var
中使用!
,以避免if let
检查。
for connection in stillImageOutput!.connections {
}