1

I was pointed to this objc snippet from WWDC 14, but I work on a Swift project.

CMIOObjectPropertyAddress prop = { kCMIOHardwarePropertyAllowScreenCaptureDevices, kCMIOObjectPropertyScopeGlobal, kCMIOObjectPropertyElementMaster }; UInt32 allow = 1; CMIOObjectSetPropertyData(kCMIOObjectSystemObject, &prop, 0, NULL, sizeof(allow), &allow); 

I then tried rewriting to Swift:

var prop : CMIOObjectPropertyAddress { kCMIOHardwarePropertyAllowScreenCaptureDevices kCMIOObjectPropertyScopeGlobal kCMIOObjectPropertyElementMaster } var allow:UInt32 = 1 CMIOObjectSetPropertyData(kCMIOObjectSystemObject, &prop, 0, nil, sizeof(UInt32), &allow) 

But it doesn't even validate. I don't know how to translate the CMIOObjectPropertyAddress struct. Xcode says

/Users/mortenjust/Dropbox/hack/learning/screenrec/screenrec/deleteme.swift:32:61: Cannot assign to a get-only property 'prop'

1
  • Oh, forgot to add, I got it working by bridging an objective-c class that does only the stuff above. I want to avoid this, as bridging headers make Xcode autocompletion incredibly slow. Commented May 6, 2015 at 2:33

2 Answers 2

3

A C struct translates as a Swift struct. Use the implicit memberwise initializer:

 var prop = CMIOObjectPropertyAddress( mSelector: UInt32(kCMIOHardwarePropertyAllowScreenCaptureDevices), mScope: UInt32(kCMIOObjectPropertyScopeGlobal), mElement: UInt32(kCMIOObjectPropertyElementMaster)) 

The cool part is when you type CMIOObjectPropertyAddress(, code completion gives you the rest.

Sign up to request clarification or add additional context in comments.

Comments

3

You're right, just got it running right this second. Turns out I also had to correct for some of the types. Here's the complete translation:

 var prop = CMIOObjectPropertyAddress( mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyAllowScreenCaptureDevices), mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal), mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMaster)) var allow : UInt32 = 1 var dataSize : UInt32 = 4 var zero : UInt32 = 0 CMIOObjectSetPropertyData(CMIOObjectID(kCMIOObjectSystemObject), &prop, zero, nil, dataSize, &allow) var session = AVCaptureSession() session.sessionPreset = AVCaptureSessionPresetHigh var devices = AVCaptureDevice.devices() for device in AVCaptureDevice.devices() { println(device) } 

1 Comment

Thanks for reporting back. Could be useful to others! (Swift numerics are maddening, aren't they?)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.