Swift UnsafeMutablePointer<Unmanaged<CFString>?> allocation and print

放肆的年华 提交于 2019-11-28 09:26:58

I have no experience with CoreMIDI and could not test it, but this is how it should work:

let midiEndPoint = MIDIGetSource(0)
var property : Unmanaged<CFString>?
let err = MIDIObjectGetStringProperty(midiEndPoint, kMIDIPropertyDisplayName, &property)
if err == noErr {
    let displayName = property!.takeRetainedValue() as String
    println(displayName)
}

As @rintaro correctly noticed, takeRetainedValue() is the right choice here because it is the callers responsibility to release the string. This is different from the usual Core Foundation memory management rules, but documented in the MIDI Services Reference:

NOTE

When passing a Core Foundation object to a MIDI function, the MIDI function will never consume a reference to the object. The caller always retains a reference which it is responsible for releasing by calling the CFRelease function.

When receiving a Core Foundation object as a return value from a MIDI function, the caller always receives a new reference to the object, and is responsible for releasing it.

See "Unmanaged Objects" in "Working with Cocoa Data Types" for more information.

UPDATE: The above code works only when compiling in 64-bit mode. In 32-bit mode, MIDIObjectRef and MIDIEndpointRef are defined as different kind of pointers. This is no problem in (Objective-)C, but Swift does not allow a direct conversion, an "unsafe cast" is necessary here:

let numSrcs = MIDIGetNumberOfSources()
println("number of MIDI sources: \(numSrcs)")
for srcIndex in 0 ..< numSrcs {
    #if arch(arm64) || arch(x86_64)
    let midiEndPoint = MIDIGetSource(srcIndex)
    #else
    let midiEndPoint = unsafeBitCast(MIDIGetSource(srcIndex), MIDIObjectRef.self)
    #endif
    var property : Unmanaged<CFString>?
    let err = MIDIObjectGetStringProperty(midiEndPoint, kMIDIPropertyDisplayName, &property)
    if err == noErr {
        let displayName = property!.takeRetainedValue() as String
        println("\(srcIndex): \(displayName)")
    } else {
        println("\(srcIndex): error \(err)")
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!