4

Getting an error here. Racking my brain. Tried all sorts of combinations.

Cannot find an initializer for type 'NSDictionary' that accepts an argument list of type '(object:(Int), forKey: CFString!)'

// configure the pixel format -  Obj-C
// videoOutput.videoSettings = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA], (id)kCVPixelBufferPixelFormatTypeKey, nil];

// Swift       
videoOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey:kCVPixelBufferPixelFormatTypeKey)
Edward
  • 241
  • 5
  • 15
  • Why is there a comma after 32BGRA and not a close paren?? the forKey is being passed there, not to the dictionary constructor.. – Rob Apr 22 '15 at 00:00
  • ok thanks. But now I'm getting these 2 errors: http://i.imgur.com/WqZy8LR.png – Edward Apr 22 '15 at 00:05

3 Answers3

12

update: Swift 5.7

videoOutput.videoSettings = [
    kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)
]
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    Wow thanks. why did it work? I don't understand it. It's like Inception. A cast within a cast within a cast :) – Edward Apr 22 '15 at 01:33
  • Works brilliantly. So it's no longer a dictionary if you remove NSDictionary? Or does the square brackets keep it a dictionary? What type is `kCVPixelBufferPixelFormatTypeKey` originally? Just a string? – Edward Apr 22 '15 at 02:21
  • It is a CFString https://developer.apple.com/library/mac/documentation/QuartzCore/Reference/CVPixelBufferRef/#//apple_ref/doc/constant_group/Pixel_Buffer_Attribute_Keys. That second version it is just a Swift Dictionary with a single key – Leo Dabus Apr 22 '15 at 02:23
  • The second version `videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String : Int(kCVPixelFormatType_32BGRA)]` is throwing a fatal error in swift 2 `fatal error: unexpectedly found nil while unwrapping an Optional value` – Ian Sep 18 '15 at 09:49
  • @lan the correct syntax is [kCVPixelBufferPixelFormatTypeKey : Int(kCVPixelFormatType_32BGRA)] – Leo Dabus Oct 19 '15 at 14:29
  • not working to me, I got this error Type of expression is ambiguous without more context – famfamfam Dec 28 '22 at 14:15
  • No wonder. This has been posted almost 8 years ago. Swift 2 – Leo Dabus Dec 28 '22 at 19:16
1

This has been changed in Swift 4.x, with the following working for me:

captureVideoDataOutput.videoSettings = 
    [kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA]
CodeBender
  • 35,668
  • 12
  • 125
  • 132
0

It looks like the type would be [String: Int].

Actually, looking in the Xcode docs, it says that the value can either be a single number or an array. If it's an array then the type would be [String: [Int]].

So you should be able to say:

let aVideoSetting: [String: Int] =
[kCVPixelBufferPixelFormatTypeKey: kCVPixelFormatType_32BGRA] videoOutput.videoSettings = aVideoSetting

I'm still not 100% with the interoperability of Cocoa types and Swift types, so that might not be perfect, but it's close.

You could also rewrite your code to create an NSDictonary in swift syntax:

let aPixelFormat = NSNumber(int: kCVPixelFormatType_32BGRA)
let settingsDict = NSDictionary(aPixelFormat 
  forKey:  "numberWithUnsignedInt:kCVPixelFormatType_32BGRA")
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Not working. This code on Git is exactly the Swift version of my Obj-C code. https://github.com/bradley/iOSSwiftOpenGLCamera/blob/master/iOSSwiftOpenGLCamera/CameraSessionController.swift. `videoDeviceOutput.videoSettings = NSDictionary(object: Int(kCVPixelFormatType_32BGRA), forKey:kCVPixelBufferPixelFormatTypeKey)`. There must be something I'm missing. – Edward Apr 22 '15 at 00:34
  • not working to me, I got this error Type of expression is ambiguous without more context – famfamfam Dec 28 '22 at 14:15
  • “This code is exactly the Swift version of my Obj-C code.” No. Every single character matters. You need to EDIT YOUR QUESTION to show your exact code, along with the specific line which is giving an error, and the exact error. – Duncan C Dec 28 '22 at 16:23