为什么“没有已知的选择器x的方法是ARC下的一个硬错误? [英] Why is 'no known method for selector x' a hard error under ARC?

查看:135
本文介绍了为什么“没有已知的选择器x的方法是ARC下的一个硬错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是有用的,如果调用一个方法MyClass不理解一个类型的MyClass是一个错误,而不是一个警告,因为它可能是一个错误或将来会导致错误...

Maybe it's useful if calling a method that MyClass doesn't understand on a something typed MyClass is an error rather than a warning since it's probably either a mistake or going to cause mistakes in the future...

但是,为什么这个错误特定于ARC? ARC根据可可内存管理约定决定需要保留/释放/自动释放,这表明知道选择器的名称就足够了。因此,将SEL变量传递给 performSelector:是有问题的,因为它在编译时不知道选择器是一个init / copy / new方法还是不。但是为什么在类接口中看到这一点或者没有什么区别呢?

However, why is this error specific to ARC? ARC decides what it needs to retain/release/autorelease based on the cocoa memory management conventions, which would suggest that knowing the selector's name is enough. So it makes sense that there are problems with passing a SEL variable to performSelector:, as it's not known at compile-time whether the selector is an init/copy/new method or not. But why does seeing this in a class interface or not make any difference?

我缺少一些关于ARC工作原理,或者是clang警告只是有点不一致?

Am I missing something about how ARC works, or are the clang warnings just being a bit inconsistent?

推荐答案


ARC根据可可内存管理惯例决定需要保留/释放/ ,这意味着知道选择器的名字就足够了。

ARC decides what it needs to retain/release/autorelease based on the cocoa memory management conventions, which would suggest that knowing the selector's name is enough.

这只是ARC决定内存管理的一种方法。 ARC还可以通过属性确定内存管理。例如,你可以使用 __ attribute __((NSObject))(永远不要这样做,但它是合法的)声明任何typedef可保留。您还可以使用其他属性,例如 __ attribute((ns_returns_retained))和其他几个来覆盖命名约定(这些是你可能合理地做的,如果你不能修复命名;

This is just one way that ARC determines memory management. ARC can also determine memory management via attributes. For example, you can declare any typedef retainable using __attribute__((NSObject)) (never, ever do this, but it's legal). You can also use other attributes like __attribute((ns_returns_retained)) and several others to override naming conventions (these are things you might reasonably do if you couldn't fix the naming; but it's much better to fix the naming).

现在,假设你没有包含在一些文件中声明这些属性的头文件,而在其他文件中没有声明这些属性的情况。现在,一些编译单元(.m文件)内存管理它的一种方式,一些内存管理它另一个。 Hijinks确保。这比没有ARC的情况要糟糕得多,因此一些ARC代码会做一件事,而其他ARC代码会做一些不同的事情。因此,所产生的错误会令人费解。

Now, imagine a case where you failed to include the header file that declares these attributes in some files but not others. Now, some compile units (.m files) memory manage it one way and some memory manage it another. Hijinks ensure. This is much, much worse than the situation without ARC, and the resulting bugs would be mindbending because some ARC code would do one thing and other ARC code would do something different.

所以,是的,不要这样做。 (当然你不应该忽略Objective-C中的警告,但这是一个特别讨厌的情况。)

So, yeah, don't do that. (Of course you should never ignore warnings in Objective-C anyway, but this is a particularly nasty situation.)

这篇关于为什么“没有已知的选择器x的方法是ARC下的一个硬错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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