I am trying to create a CGColor in Swift 5.3 in macOS Catalina 10.15.7, using:
let colour : CGColor = CGColor.init(red: r, green: g, blue: b, alpha: 10.0)
but the returned object is a NSObject. Same result as using
let colour : CGColor = NSColor.init(red: r, green: g, blue: b, alpha: 10.0).cgColor
The NSColor is created correctly but at retrieve the CGColor, the result is NSObject.
Some suggestions.
Thanks
The problem is that you are examining this object in the variable pane of the Xcode debugger. Sure enough, it is listed there just as an NSObject. But that's because it is what we call a pseudo-object. The CGColor type is not a true object type, even though Swift tries to disguise it as one; it is actually a CFType, which is an opaque struct — an Objective-C struct. You can discover this by opening the triangles:
To Objective-C, then, this isn't even an object. But for purposes of presentation and manipulation — i.e., to make it possible to pass it around as you are doing in your code — it has been wrapped up in an NSObject.
The outcome is that you can't tell anything about this thing in the variables list on the left of the debug pane. But in the right of the debug pane you can ask for a description which is useful. Thus, put a breakpoint at the
return
statement in your function, and when you pause there, say this in the console:You will see something like this: