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

时间:2022-09-18 02:03:39

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.

迅速。我有这个,但我正忙着投出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

谢谢

1 个解决方案

#1


7  

This is the signature for MIDIObjectGetProperties in Swift:

这是Swift中MIDIObjectGetProperties的签名:

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

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

所以你需要将UnsafeMutablePointer传递给Unmanaged ?:

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:

现在你有了你的属性,但是它们在一个非托管变量中 - 你可以使用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)")
}

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

#1


7  

This is the signature for MIDIObjectGetProperties in Swift:

这是Swift中MIDIObjectGetProperties的签名:

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

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

所以你需要将UnsafeMutablePointer传递给Unmanaged ?:

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:

现在你有了你的属性,但是它们在一个非托管变量中 - 你可以使用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)")
}

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