Search code examples
swiftmacosswift4macos-high-sierraxcode9.3

How to iterate over a macOS app’s windows until a condition is met?


To iterate over all windows in my macOS app, I use enumerateWindows(options:using:) like this:

NSApplication.shared.enumerateWindows(options: .orderedFrontToBack, using: { 
    (window: NSWindow, stop: UnsafeMutablePointer<ObjCBool>) in
    if let vc = window.contentViewController as? SomeCustomViewController {
        if someCondition {
            stop = true // “Cannot assign to value: 'stop' is a 'let' constant”
        }
    }
})

I want to stop the enumeration when someCondition is met, but I can’t set the UnsafeMutablePointer<ObjCBool> to true: Xcode tells me that stop is a let constant.

What am I doing wrong?


Solution

  • stop is a pointer, you have to set the pointee

    stop.pointee = true