从Objective-C到Swift:转换for循环的正确方法

马丁

我正在尝试将此代码转换为swift,但是在if语句中不断收到错误,objective-c代码如下所示:

AVCaptureStillImageOutput *stillImageOutPut;
AVCaptureConnection *videoConnection = nil;

for (AVCaptureConnection *connection in stillImageOutput.connections){
      for (AVCaptureInputPort *port in [connection inputPorts]){
          if ([[port mediaType] isEqual:AVMediaTypeVideo]){
          videoConnection = connection;
          break;
           }
      }
}

我的Swift代码如下所示:

let stillImageOutPut = AVCaptureStillImageOutput()

let videoConnection:AVCaptureConnection? = nil

        for connection in stillImageOutPut.connections{
            for port in [connection.inputPorts]{
                if
            }
        }

在if语句中,我找不到,.mediaType并且自动完成功能说descriptiongetMirrormap我尝试过以其他方式在for循环中强制转换类型,但我总是不断出错。

关于如何正确创建此for循环的任何建议将不胜感激。

抢马约夫

stillImageOutPut.connectionsNSArray在Objective-C中是一个Array<AnyObject>在Swift中是一个您将需要Array<AVCaptureConnection>在Swift中将其转换为。同样,您需要转换connection.inputPortsArray<AVCaptureInputPort>

let stillImageOutPut = AVCaptureStillImageOutput()

var videoConnection:AVCaptureConnection? = nil

for connection in stillImageOutPut.connections as [AVCaptureConnection] {
    for port in connection.inputPorts as [AVCaptureInputPort] {
        if port.mediaType == AVMediaTypeVideo {
            videoConnection = connection
        }
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章