How do you encode and decode a CGPoint struct using NSCoder?
CGPoint
s and NSPoint
s are both structures composed of two CGFloat
values, so you can freely pass them around as each other. The quick and dirty way would be:
NSCoder *myNSCoder;
CGPoint myPoint;
[myNSCoder encodePoint:*(NSPoint *)myPoint];
This will usually work, but it technically breaks the C99 strict aliasing rules. If you want to be 100% compatible with the standard, you'll have to do something like:
typedef union
{
CGPoint cgPoint;
NSPoint nsPoint;
} CGNSPoint;
CGNSPoint cgnsPoint = { .cgPoint = myPoint };
[myNSCoder encodePoint:cgnsPoint.nsPoint];