问题描述
我已经为ARC重构了一个项目。它看起来很好,但有一个使用通知中心的对象。我在自定义dealloc方法中删除了观察者。这在非ARC项目中运行良好。它也适用于ARC,但我收到一个疯狂的警告:方法可能错过了[super dealloc]调用。在ARC项目中,当方法结束时,它会自动为我完成。更好的是:我不能在ARC项目中调用它!这必须是一个XCode错误,对吗?这是我的代码:
I've refactored a project to ARC. It looks fine, but there is an object which uses the notification center. I removed the observer in a custom dealloc method. That worked fine in the non ARC project. It also works in ARC, but I get a crazy warning: "Method possibly missing a [super dealloc] call." In an ARC project it is automatically done for me, when the method ends. Even better: I must not call it in ARC projects! This must be an XCode bug, right? Here's my code:
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
// [super dealloc]; will be called automatically
}
我总是想编写不会发出警告的代码。那个黄色感叹号有办法吗?
I always want to write code that doesn't throw warnings. Is there a way around that yellow exclamation mark?
推荐答案
将以下行放入dealloc方法,以确保在启用ARC的情况下编译它:
Put the following lines into your dealloc method to make sure it is compiled with ARC enabled:
#if ! __has_feature(objc_arc)
#error "ARC is off"
#endif
如果在构建时遇到编译器错误,则确定ARC已关闭并且必须搜索原因。它可能是您目标中的每个文件构建设置。
If you get the compiler error when building, you're sure that ARC is off and have to search for the reason. It's probably in per-file build settings in your target.
这篇关于“缺少[super dealloc]” ARC项目中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!