我正在尝试将此代码转换为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
,自动填充说description
,getMirror
和map
。我已尝试以其他方式在for循环中转换类型,但我只是不断收到错误。
有关如何正确创建此for循环的任何建议都将受到赞赏。
答案 0 :(得分:3)
stillImageOutPut.connections
在Objective-C中是NSArray
,在Swift中是Array<AnyObject>
。你想把它转换成Swift中的Array<AVCaptureConnection>
。同样,您需要将connection.inputPorts
投射到Array<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
}
}
}
答案 1 :(得分:1)
丢失[connection.inputPorts]
中的括号。这不再是Objective-C了! Swift中的括号表示数组,而在Objective-C中,它们仅表示消息发送。只需将该行更改为:
for port in connection.inputPorts {
......你的世界将大大增亮。
答案 2 :(得分:0)
没有对此进行测试,但我认为它应该可行:
for connection in stillImageOutPut.connections{
for port in connection.inputPorts as [AVCaptureInputPort]{
let mediaType = port.mediaType;
}
}