-[Not A Type retain]:消息发送到已释放的实例

-[Not A Type retain]: message sent to deallocated instance(-[Not A Type retain]:消息发送到已释放的实例)
本文介绍了-[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 在 here answer here,因为从 -CGColor 返回的 CGColorRef 在您最后一次引用生成它的 UIColor.我认为这是一个错误,基于 this Apple developer forum thread 中的讨论,但它是对如何管理这些 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.

一种可行的方法是使用 UIColor 上的 -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];

其中 color1color2 是 UIColor 实例.

with color1 and color2 being UIColor instances.

-CGColor 方法会为您处理桥接,根据 过渡到 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 在上面链接的开发者论坛线程中报告的内容相匹配.

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

除了上述之外,您还可以显式保留和释放从 -CGColor 方法返回的 CGColorRefs,并在您的 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]:消息发送到已释放的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)