osx - Calling CFDictionaryContainsValue() in Swift -


i have core foundation dictionary find specific value in. naturally use cfdictionarycontainsvalue() provided core foundation. first argument core foundation dictionary search , second argument of type unsafepointer<void>. i'm looking string in dictionary tried this:

var devicetofind = "disk1" var result: kern_return_t = kern_failure let classestomatch = ioservicematching(kiomediaclass) let classestomatchdict = (classestomatch nsdictionary) as! dictionary<string, anyobject> let matcheddevices = (classestomatchdict nsdictionary) cfdictionaryref  result = ioservicegetmatchingservices(kiomasterportdefault, classestomatchcfdictref, &storageiterator);  if cfdictionarycontainsvalue(matcheddevices, &devicetofind) == true {     print("found disk1") } 

but crashes (exc_bad_access) @ call cfdictionarycontainsvalue(). according apple's documentation interacting unsafepointers i'm structuring call correctly there's problem i'm unsure of how work.

to bsd name ioreg cannot retrieve value matcheddevices directly. have iterate through storageiterator , bsd name property respectively.

var devicetofind = "disk1" var storageiterator = io_iterator_t() var object : io_object_t var result: kern_return_t = kern_failure let classestomatchdict = ioservicematching("iomedia")  result = ioservicegetmatchingservices(kiomasterportdefault, classestomatchdict, &storageiterator)  if kern_success == result && storageiterator != 0 {   repeat {     object = ioiteratornext(storageiterator)     if let data = ioregistryentrycreatecfproperty(object, kiobsdnamekey, kcfallocatordefault, 0), disk = data.takeretainedvalue() as? string {       if devicetofind == disk {         print("found \(devicetofind)")         break       }     }   } while object != 0   ioobjectrelease(storageiterator) } 

Comments