调用中的 NSDictionary 额外参数 "forKey"

NSDictionary extra argument "forKey" in call

正如我的问题所述,我在尝试编写这段代码时不断收到错误 "extra argument 'forKey' in call"。有人可以解释一下为什么吗?

var rgbOutputSettings:NSDictionary = NSDictionary(object: NSNumber(int: kCMPixelFormat_32BGRA), forKey: kCVPixelBufferPixelFormatTypeKey)

在某些情况下,如果参数的类型与函数声明的类型不匹配,即使调用看起来正确,也会给出 "Extra argument in call"。

请在 SO

上参考这个 question

因此您可以将其用作

var key = kCVPixelBufferPixelFormatTypeKey as NSString

分解您的陈述我们得到:

let obj = NSNumber(integer: kCMPixelFormat_32BGRA)
let key = kCVPixelBufferPixelFormatTypeKey
var rgbOutputSettings : NSDictionary = NSDictionary(object: obj, forKey: key)

问题是 kCVPixelBufferPixelFormatTypeKey 可能不止是一种类型。引用文档(强调我的):

kCVPixelBufferPixelFormatTypeKey:

The pixel format for this buffer (type CFNumber, or type CFArray containing an array of CFNumber types (actually type OSType)).

我已将其转换为字符串以使其编译:

let key: String = kCVPixelBufferPixelFormatTypeKey as String

也许这不是你想要的,如果你需要实现类似 this

kCVPixelBufferPixelFormatTypeKeyCFStringRef 而不是 NSString。您需要将其转换为 NSString,如下所示:

var formatKey = kCVPixelBufferPixelFormatTypeKey as NSString
var rgbOutputSettings:NSDictionary = NSDictionary(object: NSNumber(int: kCMPixelFormat_32BGRA), forKey: formatKey)