Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

to swift. I have this but I am getting hung up on casting the 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

like image 426
Nick Red Avatar asked Sep 02 '14 15:09

Nick Red


1 Answers

This is the signature for MIDIObjectGetProperties in Swift:

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

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

var midiDevice = MIDIGetDevice(0)
var unmanagedProperties: Unmanaged<CFPropertyList>?

MIDIObjectGetProperties(midiDevice, &unmanagedProperties, 1)

Now you have your properties, but they're in an unmanaged variable -- you can use the takeUnretainedValue() method to get them out, and then cast the resulting CFPropertyList to 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;
}
like image 184
Nate Cook Avatar answered Sep 18 '22 00:09

Nate Cook