- [Not A Type retain]:发送到解除分配的实例的消息 [英] -[Not A Type retain]: message sent to deallocated instance

查看:305
本文介绍了 - [Not A Type retain]:发送到解除分配的实例的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的应用转换为使用ARC。

I have converted my app to use ARC.

在我使用以下代码行之前:

Before I had the following line of code:

NSArray *colors = [NSArray arrayWithObjects:startColor, endColor, nil];

由于ARC不允许将非Objective-C指针类型隐式转换为'id' ,我重写这样的行:

Since the implicit conversion of a non-Objective-C pointer type to 'id' is disallowed with ARC, I rewrote the line like this:

NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];

在模拟器上一切正常,但是在设备上应用程序崩溃了上述行并出现错误消息:

Everything works fine on the simulator, however on the device the app crashes on the mentioned line with the error message:

-[Not A Type retain]: message sent to deallocated instance

任何想法如何解决?

推荐答案

此桥接投可能无法正常工作,如在hatfinch 他的答案在这里,因为从 -CGColor 返回的CGColorRef在你最后一次引用生成它的UIColor后可能不会挂起。我认为这是一个错误,基于这个Apple开发者论坛帖子中的讨论,但它误读了如何管理这些CGColorRefs的生命周期。

This bridged cast may not work, as hatfinch describes in his answer here, because the CGColorRef returned from -CGColor may not hang around after your last reference to the UIColor that generates it. I thought this was a bug, based on the discussion in this Apple developer forum thread, but it was a misreading of how to manage the lifetime of these CGColorRefs.

这种方法的一种方法是使用 -CGColor 方法。不像上面那样将CGColor保存到临时变量,而是应该能够使用如下内容:

One way that this will work is to use the built-in bridging provided by the -CGColor method on UIColor. Rather than saving out your CGColor to a temporary variable as you do above, you should be able to use something like the following:

NSArray *colors = [NSArray arrayWithObjects:(id)[color1 CGColor],
                                            (id)[color2 CGColor], nil];

color1 color2 是UIColor实例。

with color1 and color2 being UIColor instances.

通过 -CGColor 方法,根据的编译器处理CF对象返回的从可可方法的过渡到ARC发行说明。该文档目前缺少我上面的转换为id,这是编译所需的。

The bridging is taken care of for you by the -CGColor method, according to the "The Compiler Handles CF Objects Returned From Cocoa Methods" section of the Transitioning to ARC Release Notes. The documentation is currently missing the cast to id that I have above, which is required to get this to compile.

我已经测试了这个,它似乎可以工作在我的情况下,匹配Ben在上面链接的Developer Forums线程中报告的内容。

I've tested this, and it seems to work in my case, matching what Ben reports in the above-linked Developer Forums thread.

除了上述内容,您还可以显式保留并释放从<$返回的CGColorRefs c $ c> -CGColor 方法并在NSArray中将它们连接起来,再次当hatfinch显示这里

In addition to the above, you can explicitly retain and release the CGColorRefs returned from the -CGColor method and bridge them across in your NSArray, again as hatfinch shows here.

这篇关于 - [Not A Type retain]:发送到解除分配的实例的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆