I have a class for a shadowed table view that I am using in my app.
When doing the conversion to ARC, I had to change some things. With the new class, it is crashing at the following lines: (id)(inverse ? darkColor : lightColor) to (__bridge id)(inverse ? lightColor : darkColor). The console says *** -[Not A Type retain]: message sent to deallocated instance 0x4cee70
Before:
- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
CAGradientLayer *newShadow = [[[CAGradientLayer alloc] init] autorelease];
CGRect newShadowFrame =
CGRectMake(0, 0, self.frame.size.width,
inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
newShadow.frame = newShadowFrame;
CGColorRef darkColor =
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25].CGColor;
CGColorRef lightColor =
[self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
newShadow.colors =
[NSArray arrayWithObjects:
(id)(inverse ? lightColor : darkColor),
(id)(inverse ? darkColor : lightColor),
nil];
return newShadow;
}
After:
- (CAGradientLayer *)shadowAsInverse:(BOOL)inverse
{
CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
CGRect newShadowFrame =
CGRectMake(0, 0, self.frame.size.width,
inverse ? SHADOW_INVERSE_HEIGHT : SHADOW_HEIGHT);
newShadow.frame = newShadowFrame;
CGColorRef darkColor =
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25].CGColor;
CGColorRef lightColor =
[self.backgroundColor colorWithAlphaComponent:0.0].CGColor;
newShadow.colors =
[NSArray arrayWithObjects:
(__bridge id)(inverse ? lightColor : darkColor),
(__bridge id)(inverse ? darkColor : lightColor),
nil];
return newShadow;
}
Apple’s Transitioning to ARC Release Notes has a subsection titled “The Compiler Handles CF Objects Returned From Cocoa Methods”, which uses
CAGradientLayer.colorsas an example. It says this code will work as-is:So maybe you should try rewriting your code like this: