Need help converting (CFPropertyListRef *)nsdictionary to swift
Asked Answered
B

1

7

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

Beatify answered 2/9, 2014 at 15:11 Comment(1)
You're using constructed object in constant. You need a pointer to NSDictionary, so MIDIObjectGetProperties could create object and overwrite it thus returning a value.Disbud
B
10

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;
}
Bartholomeus answered 2/9, 2014 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.