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 import
ed 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?
kCGWindowImageDefault
is only for Objective-C, not Swift. In Swift,CGWindowImageOption
is anOptionSet
. For the default you simply use an empty option set:For
CGWindowListOption
you can do:Be sure you look at the Swift reference documentation for these enumerations. You can't use the Objective-C values.