为什么ARC禁止调用未声明的方法? [英] Why ARC forbids calls to undeclared methods?

查看:105
本文介绍了为什么ARC禁止调用未声明的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用手动内存管理时,我们可以编写对未在类中声明的方法的调用.在这种情况下,我们在编译过程中得到的只是警告.这是Wikipedia对Objective-C最独特的功能之一的看法:

When using manual memory management we can write a call to a method which is not declared in the class. What we get during compilation in this case is warning only. This is what Wikipedia states on one of the most distinctive Objective-C features:

面向对象编程的Objective-C模型基于传递给对象实例的消息.在Objective-C中,不只是简单地调用方法,而是调用方法.一个发送一条消息.这与C ++使用的Simula风格的编程模型不同.这两个概念之间的区别在于,如何执行由方法或消息名称引用的代码.在Simula风格的语言中,方法名称在大多数情况下由编译器绑定到目标类中的代码段.在Smalltalk和Objective-C中,消息的目标是在运行时解析的,接收对象本身会解释消息

The Objective-C model of object-oriented programming is based on message passing to object instances. In Objective-C one does not simply call a method; one sends a message. This is unlike the Simula-style programming model used by C++. The difference between these two concepts is in how the code referenced by the method or message name is executed. In a Simula-style language, the method name is in most cases bound to a section of code in the target class by the compiler. In Smalltalk and Objective-C, the target of a message is resolved at runtime, with the receiving object itself interpreting the message

那么,为什么在ARC中出现编译错误?破坏语言强大功能的原因是什么?我不知道的重要内容是什么?有人可以解释吗?谢谢

So why it is compilation error in ARC? What are the reasons to break such a powerful feature of the language? What's so important about that i'm not aware of? Can anybody explain this? Thanks

推荐答案

在此讨论中有一些有关它的信息:

There is some information about it in this discussion:

在ARC下,编译器现在需要准确地拥有所有权 方法返回.在这种情况下的默认值是该对象不是 由调用方拥有,但是在实际的声明中,对象可能是 相反,由调用方拥有("ns_returns_retained"属性) 可能拥有诸如newBar之类的拥有方法,该方法返回一个未保留的 对象("ns_returns_not_retained"属性),在前一种情况下, 在后者中得到释放不足,而您得到了过度释放,即 不利于ARC.

Under ARC, the compiler needs to now exactly the kind of ownership the method returns. The default in this case is that the object is not owned by the caller, but in the actual declaration the object may be owned by the caller ("ns_returns_retained" attribute), conversely, you may have a owning method like newBar that returns a non-retained object ("ns_returns_not_retained" attribute), in the former case you get an under-release in the latter you get an over-release, which is detrimental to ARC.

要使ARC具有确定性,编译器必须承担很多事情 因此在ARC中将以前可以进行的某些行为视为非法, 确保行为是一致的.

To make ARC deterministic, the compiler has to assume a lot of things so certain behaviours that were okay before are made illegal in ARC to ensure that the behaviour is consistent.

此回复似乎来自一位苹果开发人员:

And this response seems to be from one of the Apple developers:

我们的推理在(1)之间需要大约50/50的分配,需要更多 谨慎对待类型和所有权,以及(2)希望消除 语言令人尴尬的疣(不允许抱怨 完全未知的方法,比 警告).确实没有合法的理由来调用方法 甚至没有在某处声明.做到这一点的能力使一些 真正琐碎的错误(例如选择器中的错字)运行时失败 编译失败.我们一直对此有所警告.修改您的代码.

Our reasoning was split about 50/50 between (1) needing to be more careful about types and ownership and (2) wanting to eliminate an embarrassing wart in the language (not being allowed to complain about completely unknown methods with anything more strenuous than a warning). There is really no legitimate reason to call a method that's not even declared somewhere. The ability to do this makes some really trivial bugs (e.g. typos in selectors) runtime failures instead of compile failures. We have always warned about it. Fix your code.

所以主要的论据是编译器需要知道 返回值.

So the main argument is that the compiler needs to know the ownership of the return value.

这篇关于为什么ARC禁止调用未声明的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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