为什么我在 ARC 下比较桥接 CGColorRef 时遇到崩溃?
我有一个用于在我的应用程序中使用的阴影表视图的类。
当转换为 ARC 时,我必须改变一些东西。对于新类,它在以下几行崩溃:(id)(inverse ? darkColor : lightColor)
到 (__bridge id)(inverse ? lightColor : darkColor)
。控制台显示 *** -[Not A Type keep]: 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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Apple 的过渡到 ARC 发行说明有一个标题为“编译器处理从 Cocoa 方法返回的 CF 对象”的小节,其中使用
CAGradientLayer.colors
作为示例。它说这段代码将按原样工作:所以也许你应该尝试像这样重写你的代码:
Apple's Transitioning to ARC Release Notes has a subsection titled "The Compiler Handles CF Objects Returned From Cocoa Methods", which uses
CAGradientLayer.colors
as an example. It says this code will work as-is:So maybe you should try rewriting your code like this:
我用另一种方式解决了这个问题:
在 Xcode 中,在目标 Build Phases 选项卡中,打开组以显示 Sources Compile 源文件列表。双击您想要为九月标志的文件,在弹出面板中输入-fno-objc-arc,然后单击完成。
这会禁用 ARC 文件只会给您带来问题。
I solved this problem in another way:
In Xcode, in the target Build Phases tab, open the group to reveal Sources Compile the source file list. Double-click the file you want to for Which the flag in September, enter-fno-objc-arc in the pop-up panel, Then click Done.
This disables the ARC file only gives you problem.