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

时间:2014-09-02 15:11:03

标签: objective-c macos swift

我需要一些帮助来转换这个

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

MIDIObjectGetProperties(midiDevice, (CFPropertyListRef *)&midiProperties, YES);
NSLog(@"Midi properties: %d \n %@", i, midiProperties);
很快。我有这个,但我正忙着投出CFPropertList。

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

 MIDIObjectGetProperties(midiDevice,  CFPropertyListRef(midiProperties), 1);
println("Midi properties: \(index) \n \(midiProperties)");

任何帮助都会很棒。

由于

1 个答案:

答案 0 :(得分:7)

这是Swift中MIDIObjectGetProperties的签名:

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

因此,您需要将UnsafeMutablePointer传递给Unmanaged<CFPropertyList>?

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

MIDIObjectGetProperties(midiDevice, &unmanagedProperties, 1)

现在您拥有自己的属性,但它们位于非托管变量中 - 您可以使用takeUnretainedValue()方法将其删除,然后将生成的CFPropertyList强制转换为{{ 1}}:

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)")
}