为什么我在 ARC 下比较桥接 CGColorRef 时遇到崩溃?

发布于 2024-12-10 07:16:30 字数 1922 浏览 0 评论 0原文

我有一个用于在我的应用程序中使用的阴影表视图的类。

当转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-12-17 07:16:30

Apple 的过渡到 ARC 发行说明有一个标题为“编译器处理从 Cocoa 方法返回的 CF 对象”的小节,其中使用 CAGradientLayer.colors 作为示例。它说这段代码将按原样工作:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        CAGradientLayer *gradientLayer = (CAGradientLayer *)[self layer];
        gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor] CGColor],
                                                         (id)[[UIColor lightGrayColor] CGColor], nil];
        gradientLayer.startPoint = CGPointMake(0.0, 0.0);
        gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    }
    return self;
}

所以也许你应该尝试像这样重写你的代码:

- (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;
    UIColor darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25];
    UIColor lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0];
    newShadow.colors = inverse
        ? [NSArray arrayWithObjects:[lightColor CGColor], [darkColor CGColor], nil]
        : [NSArray arrayWithObjects:[darkColor CGColor], [lightColor CGColor], 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.colors as an example. It says this code will work as-is:

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        CAGradientLayer *gradientLayer = (CAGradientLayer *)[self layer];
        gradientLayer.colors = [NSArray arrayWithObjects:(id)[[UIColor darkGrayColor] CGColor],
                                                         (id)[[UIColor lightGrayColor] CGColor], nil];
        gradientLayer.startPoint = CGPointMake(0.0, 0.0);
        gradientLayer.endPoint = CGPointMake(1.0, 1.0);
    }
    return self;
}

So maybe you should try rewriting your code like this:

- (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;
    UIColor darkColor =
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:
            inverse ? (SHADOW_INVERSE_HEIGHT / SHADOW_HEIGHT) * 0.25 : 0.25];
    UIColor lightColor =
        [self.backgroundColor colorWithAlphaComponent:0.0];
    newShadow.colors = inverse
        ? [NSArray arrayWithObjects:[lightColor CGColor], [darkColor CGColor], nil]
        : [NSArray arrayWithObjects:[darkColor CGColor], [lightColor CGColor], nil];
    return newShadow;
}
甜嗑 2024-12-17 07:16:30

我用另一种方式解决了这个问题:

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文