I get confused sometimes when working with CG elements, and have the following scenario that seems simple but is giving me trouble.
I have a CGColorRef property called fillColor, for which I manually define the setter method as follows:
@property(nonatomic) CGColorRef fillColor;
- (void) setFillColor:(CGColorRef)fillColor
{
CGColorRetain(fillColor);
CGColorRelease(_fillColor);
_fillColor = fillColor;
}
After I _fillColor to some value, I store it in an array as follows:
_fillColors = [[NSArray alloc] initWithObjects:(id)_fillColor, nil];
I’d like it so that when change self.fillColor, I’d like it for the value in the array to change also. I could obviously make a pointer, ie CGColorRef *fillColor, but in doing that how would I manage the memory (ie release, retain it) and how would my setter method change? This is really confusing me.
NSArraycopies its objects.NSArraywill receive a copy of the pointer you pass in. That means you get a copy of a pointer to aCGColorstruct (Yes,CGColorRefis already a pointer). So changing either one will never have any effect on the other. Storing a pointer to theCGColorRefseems to be the only way, other than usingNSPointerArray(which is only iOS 6+).EDIT: The end result was making a class that contained all the information in one place (path, color, etc) and storing a pointer to that instead.