I'm trying to write a program that will scan for available serial ports and present them in a popup menu. Why can I not take the CFString
straight from the IORegistryEntryCreateCFProperty()
function and add it to the menu via string interpolation in the next line? For some reason my variable declaration is met with the error:
"NSString is not a subtype of CFString".
import Foundation
import Cocoa
import IOKit
import IOKit.serial
@objc class Serial {
init() {
}
@IBOutlet var serialListPullDown : NSPopUpButton!
func refreshSerialList(defaultprompt: String) {
let masterPort: mach_port_t = kIOMasterPortDefault
let classesToMatch: CFDictionary = IOServiceMatching(kIOSerialBSDServiceValue).takeUnretainedValue()
var matchingServices: io_iterator_t = 0
// remove everything from the pull down list
serialListPullDown?.removeAllItems()
// ask for all the serial ports
let kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, &matchingServices)
if kernResult == KERN_SUCCESS {
// success
while (io_object_t() == IOIteratorNext(matchingServices)) {
var serialport = IORegistryEntryCreateCFProperty(io_object_t(), kIOCalloutDeviceKey, kCFAllocatorDefault, 0)
serialListPullDown?.addItemWithTitle("\(serialport)")
}
}
else {
// error
}
}
}