Use of unresolved identifier: kCGWindowImageDefault (and other Core Graphics constants)

547 views Asked by At

I'm new to Swift development. I'm writing an app for MacOS (not iOS) in Swift, and am trying to adapt some of the code from this Apple sample Objective-C project for use in my program.

The problem I'm hitting is that certain Apple-defined constants such as kCGWindowImageDefault and kCGWindowListOptionAll are causing XCode to report the compile-time error "Use of unresolved identifier [identifier]".

Somewhat surprisingly, though, if I right-click kCGWidowImageDefault and select "Jump to Definition", XCode does jump to the definition of that constant (in CoreGraphics > CGWindow.h > CGWindowImageOption) -- so XCode does seem to know what that constant is.

Here are the relevant snippets of my ViewController.swift file:

import Cocoa
import CoreGraphics

class ViewController: NSViewController {

...

    func myFunction() {

        // *** XCode reports the error on kCGWindowImageDefault on this line:
        let imageOptions : CGWindowImageOption = kCGWindowImageDefault 

        ...
    }
}

The Apple documentation (as linked above) doesn't indicate what needs to be imported for these constants to be used.

XCode does appear to successfully recognize the types I'm using such as CGWindowImageOption -- it's just the constants that it isn't recognizing.

What do I need to do in order to successfully be able to use kCGWindowImageDefault and similar constants in my Swift MacOS program?

1

There are 1 answers

0
rmaddy On BEST ANSWER

kCGWindowImageDefault is only for Objective-C, not Swift. In Swift, CGWindowImageOption is an OptionSet. For the default you simply use an empty option set:

let imageOptions : CGWindowImageOption = []

For CGWindowListOption you can do:

let listOptions: CGWindowListOption = .optionAll

Be sure you look at the Swift reference documentation for these enumerations. You can't use the Objective-C values.