CGColorRef is NOT a struct

The documentation for CGColorRef (https://developer.apple.com/documentation/coregraphics/cgcolorref?language=objc) clearly shows that it is a struct. However, when I try to store a cell's border color using CGColorRef originalColor = self.bg.layer.borderColor and inspect what happens in the debugger, both that property and its copy have the same address. And later when I try to restore the border color the copy still has the same address but is no longer valid and causes a crash on assignment (originalColor is actually an instance variable...)

This is all object behavior, not struct behavior. If CGColorRef really was a struct, the contents would have been copied, the instance variable would have had its own address that would never have changed, and the value would have remained valid indefinitely and let me copy it back without a problem.

Why is this documented wrong? Was this a recent change? I actually had this code working at some point, and now it's broken.

Answered by endecotp in 734646022

The documentation for CGColorRef (https://developer.apple.com/documentation/coregraphics/cgcolorref?language=objc) clearly shows that it is a struct. 

Right, that should surely say

typedef struct CGColor* CGColorRef;

The same issue seems to appear here:

https://developer.apple.com/documentation/coregraphics/cgcolorspaceref?language=objc https://developer.apple.com/documentation/coregraphics/cgcontextref?language=objc https://developer.apple.com/documentation/coregraphics/cgfontref?language=objc etc. etc.

Presumably this is something gone wrong with the tool that extracts the documentation from the header files.

File a bug?

I'm not expert in objc, but struct in objc and Swift are very different beasts :

https://stackoverflow.com/questions/55213078/what-is-the-difference-between-swift-structs-and-objective-c-structs

https://robopress.robotsandpencils.com/understanding-reference-vs-value-types-in-objective-c-and-swift-5696845e8c9

AFAIU, struct in objc are in fact reference values as well.

Note that in Swift, CGColor is a class.

Accepted Answer

The documentation for CGColorRef (https://developer.apple.com/documentation/coregraphics/cgcolorref?language=objc) clearly shows that it is a struct. 

Right, that should surely say

typedef struct CGColor* CGColorRef;

The same issue seems to appear here:

https://developer.apple.com/documentation/coregraphics/cgcolorspaceref?language=objc https://developer.apple.com/documentation/coregraphics/cgcontextref?language=objc https://developer.apple.com/documentation/coregraphics/cgfontref?language=objc etc. etc.

Presumably this is something gone wrong with the tool that extracts the documentation from the header files.

File a bug?

CGColor* means you declare CGColorRef as a pointer to a CGColor struct. Hence a reference.

https://stackoverflow.com/questions/581689/what-does-the-asterisk-mean-in-objective-c

Sorry, I did not know your level of knowledge in objc (which may well be higher than mine !).

Your are right, I now see in doc

typedef struct CGColor CGColorRef;

However I'm quite sure I saw something different yesterday, as

typedef struct CGColor* CGColorRef;

Did they change it in the meantime or did I dream ?

This isn't fixed, after 3 months.

I'm just looking at another documentation bug which I reported in July 2022 (FB10566903) that hasn't been fixed.

Did the developer documentation team all get fired, or something?

Come on Apple. We need the documentation to be accurate. It's really difficult to write code when we can't be sure if the bug is in our code, your code, or your documentation.

CGColorRef is NOT a struct
 
 
Q