I'm working on a Swift app for macOS, where I take input from MIDI devices and transform that input. The MIDI devices being supported come in for flavors.What my code does is scan for MIDI devices, filter all the MIDI devices to only leave the ones I want, initiates classes on them and puts them in a array.However, what I'm wondering is, does my code do this efficiently, or is there a better/more (memory?) efficient way to do this?import Cocoaimport CoreMIDIclass DeviceManager {    var connectedDevices : [String : DeviceProtocol] = [ : ]    let deviceNames : [String] = [        wob,        wiggle,        drum,        scan    ]    /**     * Constructor: Scan for devices.     */    init() {        self.scan()    }    /**     * Scan for OWOW midi devices. Initiate classes on them.     */    func scan() {        let allDestinations : [MIDIEndpointRef : String] = getSourceNames()        for (midiReference, name) in allDestinations {            for deviceName in self.deviceNames {                if name.lowercased().range(of: deviceName) != nil {                    var device : (Any)? = nil                    switch name.lowercased() {                    case wob :                        device = Wob(src: midiReference)                    case wiggle :                        device = Wiggle(src: midiReference)                    case scan :                        device = Scan(src: midiReference)                    case drum :                        device = Drum(src: midiReference)                    default :                        device = nil                    }                    self.connectedDevices[formatDisplayName(name: name)] = device as? DeviceProtocol                }            }        }    }    /**     * Remove other keywords (e.g. bluetooth) from the name of the device.     *     * @param String name: The name we wish to format.     * @return String: The formatted name.     */    private func formatDisplayName(name : String) -> String {        let nameList = name.characters.split {$0 ==  }        return String(nameList[0])    }    /**     * Format the displayname of a MIDI device to something we can actually use.     *     * @param MIDIObjectRef object: A midi object     * @return String: The name of the device.     */    private func getDisplayName(_ object: MIDIObjectRef) -> String {        var param : Unmanaged<CFString>?        var name : String =         let error : OSStatus = MIDIObjectGetStringProperty(object, kMIDIPropertyDisplayName, &param)        if error == OSStatus(noErr) {            name = param!.takeRetainedValue() as String        }        return name    }    /**     * Get a list of all MIDI sources.     *     * @return [MIDIEndpointRef : String]: A dictionary of all midi endpoints.     */    private func getSourceNames() -> [MIDIEndpointRef : String] {        var names : [MIDIEndpointRef : String] = [:]        let midiCount : Int = MIDIGetNumberOfDestinations()        for i in 0..<midiCount {            let endpoint : MIDIEndpointRef = MIDIGetSource(i)            if endpoint != 0 {                names[endpoint] = getDisplayName(endpoint)            }        }        return names;    }    /**     * Return a dictionary of connected devices/     *     * @return [String : DeviceProtocol] : A dictionary of connected OWOW devices.     */    func getConnectedDevices() -> [String : DeviceProtocol] {        return self.connectedDevices    }}
Find MIDI devices with certain names and make them usable
swift;swift3;music;macos