Need help converting (CFPropertyListRef *) nsdictionary to swift

I need a little help converting this

MIDIDeviceRef midiDevice = MIDIGetDevice(i); NSDictionary *midiProperties; MIDIObjectGetProperties(midiDevice, (CFPropertyListRef *)&midiProperties, YES); NSLog(@"Midi properties: %d \n %@", i, midiProperties); 

fast. I have this, but I am getting stuck on casting a CFPropertList.

  var midiDevice = MIDIGetDevice(index) let midiProperties = NSDictionary() MIDIObjectGetProperties(midiDevice, CFPropertyListRef(midiProperties), 1); println("Midi properties: \(index) \n \(midiProperties)"); 

Any help would be great.

thanks

+6
source share
1 answer

This is the signature for MIDIObjectGetProperties in Swift:

 func MIDIObjectGetProperties(obj: MIDIObjectRef, outProperties: UnsafeMutablePointer<Unmanaged<CFPropertyList>?>, deep: Boolean) -> OSStatus 

So you need to pass UnsafeMutablePointer to Unmanaged<CFPropertyList>? :

 var midiDevice = MIDIGetDevice(0) var unmanagedProperties: Unmanaged<CFPropertyList>? MIDIObjectGetProperties(midiDevice, &unmanagedProperties, 1) 

Now you have your properties, but they are in an unmanaged variable - you can use the takeUnretainedValue() method to pull them out, and then draw the resulting CFPropertyList on an NSDictionary :

 if let midiProperties: CFPropertyList = unmanagedProperties?.takeUnretainedValue() { let midiDictionary = midiProperties as NSDictionary println("Midi properties: \(index) \n \(midiDictionary)"); } else { println("Couldn't load properties for \(index)") } 

Results:

 Midi properties: 0 { "apple.midirtp.errors" = <>; driver = "com.apple.AppleMIDIRTPDriver"; entities = ( ); image = "/Library/Audio/MIDI Drivers/AppleMIDIRTPDriver.plugin/Contents/Resources/RTPDriverIcon.tiff"; manufacturer = ""; model = ""; name = Network; offline = 0; scheduleAheadMuSec = 50000; uniqueID = 442847711; } 
+7
source

Source: https://habr.com/ru/post/974696/


All Articles