类型'OSType'不符合Swift 2.0中的协议'AnyObject'

hie*_*v92 9 swift avcaptureoutput xcode7 swift2

我刚刚使用Swift 2.0更新到Xcode 7 beta.当我将我的项目更新到Swift 2.0时,我得到了这个错误:"类型'OSType'不符合Swift 2.0中的协议'AnyObject'".我的项目在Swift 1.2中完美运行.这是代码得到错误:

videoDataOutput = AVCaptureVideoDataOutput()
        // create a queue to run the capture on
        var captureQueue=dispatch_queue_create("catpureQueue", nil);
        videoDataOutput?.setSampleBufferDelegate(self, queue: captureQueue)

        // configure the pixel format            
        **videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA]** // ERROR here!

        if captureSession!.canAddOutput(videoDataOutput) {
            captureSession!.addOutput(videoDataOutput)
        }
Run Code Online (Sandbox Code Playgroud)

我试图将kCVPixelFormatType_32BGRA转换为AnyObject,但它不起作用.有人可以帮帮我吗?对不起,我的英语不好!谢谢!

Ban*_*ngs 33

这是Swift 1.2中kCVPixelFormatType_32BGRA定义:

var kCVPixelFormatType_32BGRA: Int { get } /* 32 bit BGRA */
Run Code Online (Sandbox Code Playgroud)

这是它在Swift 2.0中的定义:

var kCVPixelFormatType_32BGRA: OSType { get } /* 32 bit BGRA */
Run Code Online (Sandbox Code Playgroud)

实际上OSType是一个UInt32不能隐式转换为NSNumber:

在编写时let ao: AnyObject = Int(1),它并不是真正将Int放入AnyObject中.相反,它隐式地将你的Int转换为NSNumber,这是一个类,然后把它放入.

/sf/answers/2024424531/

试试这个:

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)]
Run Code Online (Sandbox Code Playgroud)

要么

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)
Run Code Online (Sandbox Code Playgroud)