gpt4 book ai didi

swift - 类型 'OSType' 不符合 Swift 2.0 中的协议(protocol) 'AnyObject'

转载 作者:IT王子 更新时间:2023-10-29 05:39:00 24 4
gpt4 key购买 nike

我刚刚使用 Swift 2.0 更新到 Xcode 7 beta。当我将我的项目更新到 Swift 2.0 时,我收到了这个错误:“Type 'OSType' does not conform to protocol 'AnyObject' in Swift 2.0”。我的项目在 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)
}

我尝试将 kCVPixelFormatType_32BGRA 转换为 AnyObject,但没有成功。任何人都可以帮助我吗?对不起,我的英语不好!谢谢!

最佳答案

这是 Swift 1.2 中的 kCVPixelFormatType_32BGRA 定义:

var kCVPixelFormatType_32BGRA: Int { get } /* 32 bit BGRA */

这是它在 Swift 2.0 中的定义:

var kCVPixelFormatType_32BGRA: OSType { get } /* 32 bit BGRA */

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

When you write let ao: AnyObject = Int(1), it isn’t really putting an Int into an AnyObject. Instead, it’s implicitly converting your Int into an NSNumber, which is a class, and then putting that in.

https://stackoverflow.com/a/28920350/907422

那么试试这个:

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: Int(kCVPixelFormatType_32BGRA)]

videoDataOutput?.videoSettings = [kCVPixelBufferPixelFormatTypeKey: NSNumber(unsignedInt: kCVPixelFormatType_32BGRA)

关于swift - 类型 'OSType' 不符合 Swift 2.0 中的协议(protocol) 'AnyObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31065350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com