The code bellow seems to do the same with less code. I found the original solution to be a bit cryptic and language specific. The provided code bellow is also easier to modulate, one could imagine getting the backMostWinOfType with a single char change in the sort algorithm. One could argue that running a sort algorithm is superfluous to this task, but it's unlikely to hamper performance as you never have that many windows anyway. Thoughts and corrections are more than welcome.


    /**
     * Returns the front most window in NSApp of a spedific class or protocol type
     */
    class func frontMostWinOfType<T:NSWindow>(type:T.Type)-> T?{
        var windows:Array<T> = []
        for window : NSWindow in NSApp.windows { if(window as? T != nil) {windows.append(window as! T)}}
        windows.sortInPlace { (a, b) -> Bool in return a.orderedIndex > b.orderedIndex}
        return windows[0]
    }