需要帮助转换(CFPropertyListRef*)nsdictionary到swift

Nic*_*ood 6 macos objective-c swift

我需要一点帮助来转换它

 MIDIDeviceRef midiDevice = MIDIGetDevice(i);
NSDictionary *midiProperties;

MIDIObjectGetProperties(midiDevice, (CFPropertyListRef *)&midiProperties, YES);
NSLog(@"Midi properties: %d \n %@", i, midiProperties);
Run Code Online (Sandbox Code Playgroud)

迅速.我有这个,但我正忙着投出CFPropertList.

 var midiDevice = MIDIGetDevice(index)
let midiProperties =  NSDictionary()

 MIDIObjectGetProperties(midiDevice,  CFPropertyListRef(midiProperties), 1);
println("Midi properties: \(index) \n \(midiProperties)");
Run Code Online (Sandbox Code Playgroud)

任何帮助都会很棒.

谢谢

Nat*_*ook 7

这是MIDIObjectGetPropertiesSwift中的签名:

func MIDIObjectGetProperties(obj: MIDIObjectRef, outProperties: UnsafeMutablePointer<Unmanaged<CFPropertyList>?>, deep: Boolean) -> OSStatus
Run Code Online (Sandbox Code Playgroud)

所以你需要传递UnsafeMutablePointerUnmanaged<CFPropertyList>?:

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

MIDIObjectGetProperties(midiDevice, &unmanagedProperties, 1)
Run Code Online (Sandbox Code Playgroud)

现在你有你的属性,但是它们在一个非托管变量中 - 你可以使用该takeUnretainedValue()方法将它们取出,然后将结果CFPropertyList转换为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)")
}
Run Code Online (Sandbox Code Playgroud)

结果:

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;
}
Run Code Online (Sandbox Code Playgroud)