[swift-users] IOKit and USB devices with swift 3

Quinn "The Eskimo!" eskimo1 at apple.com
Mon Sep 26 05:16:37 CDT 2016

On 26 Sep 2016, at 10:48, Quinn The Eskimo! via swift-users <swift-users at swift.org> wrote:

> Next, unless you absolutely have to, I’d avoid getting between IOServiceMatching and IOServiceAddMatchingNotification.  These two have unusual memory management behaviour (IOServiceMatching returns a +1 reference and IOServiceAddMatchingNotification consumes that reference).  It looks like the current SDK has the right annotations for this but it’s still easy to run into trouble.

And, and if you do get between these two it’s probably easiest to work in ‘CFDictionary space’ rather than try to treat this stuff as a Swift dictionary.  This will help:

func CFDictionarySetValue(_ dict: CFMutableDictionary, _ key: String, _ valueObj: AnyObject) {
    let keyObj = key as NSString
    withExtendedLifetime(keyObj) {
        CFDictionarySetValue(dict, Unmanaged.passUnretained(keyObj).toOpaque(), Unmanaged.passUnretained(valueObj).toOpaque())

Share and Enjoy
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

More information about the swift-users mailing list