什么是“超级”在Objective-C? (自我!=超级)? [英] What is "super" in Objective-C? ( self != super )?

查看:84
本文介绍了什么是“超级”在Objective-C? (自我!=超级)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了下面的问题,故事 SEEMS 简单:

I've read the question below, and the story SEEMS simple:

Objective-C究竟是什么超级?

然而...

- (id) init
{
    NSLog(@"self=%p, super=%p", self, super);
}

打印出self = 0xa83dc50,super = 0xbfffe8d0。地址不是相同的???!?!?

That prints out "self=0xa83dc50, super=0xbfffe8d0". The addresses are NOT THE SAME???!?!?

第二个地址看起来像是特殊价值或其他东西。这是什么意思?

That second address seems like a "special value" or something. What does it mean?

感谢bbum指出这个值是编译器用来实现的特殊结构的堆栈地址超级行为。

Thanks to bbum for pointing out that this value is the stack address of a special struct used by the compiler to implement the "super" behavior.

我可以调用[super init]和电话似乎有效,或至少没有爆炸......不是立刻。使用EXC_BAD_ACCESS调用[((id)0xbfffe8d0)init]失败。

I can call [super init] and the call seems to work, or at least, nothing explodes ... not immediately. Calling [((id)0xbfffe8d0) init] fails hard with EXC_BAD_ACCESS.

然后有真正的WEIRD部分.....
我有一段代码,由于没有可解释的原因抛出NSGenericException:集合在被枚举时被突变异常。在一个DIFFERENT对象(基本上是一个包含指向NSEnumerator的指针的包装器)中,注释掉对[super init]的调用会导致异常不会发生。如果可以的话,我会为这个心灵弯曲者的答案提出一个$$$奖励。

Then there is the REALLY WEIRD part..... I've got a piece of code that for no explainable reason throws a "NSGenericException: collection was mutated while being enumerated" exception. Inside a DIFFERENT object (basically a wrapper that has a pointer to the NSEnumerator), commenting out the call to "[super init]" causes the exception to not happen. If I could, I'd put out a $$$ reward for an answer to THAT mind-bender.

id sups =(id)0xbfffe8d0......这也导致收藏被修改。 ...... WTF?好的,所以我发布第二个问题就是那个bizzariotity ...

"id sups = (id)0xbfffe8d0" ... that also leads to "collection is modified." ... WTF? Ok, so I'm posting a 2nd question for that bizzariotity ...



我原来带着其中一个问题来到这里奇怪的症状错误,结果证明完全不相关(典型的这类事情):将地址转换为(id)是否有副作用???地址0xbfffe8d0特殊吗? (已修复:问题出在_NSCallStackArray上)

但是,该行上方的内容仍然有效,响应仍然很好。如果你想更深入地了解ObjC,请阅读它。

However, the content above the line is still valid, and the response still excellent. Read it if you want to understand ObjC just a little bit deeper.

推荐答案

你正在弄乱窗帘后面的男人和他正在惩罚你...... :)

You are messing with the man behind the curtain and he is punishing you for it... :)

super 真的是编译魔术。当你说 [super doSomething] 时,编译器将发出对 objc_msgSendSuper()的调用,而不是 objc_msgSend()。大部分时间 - 有一些特殊情况。

super is a bit of compiler magic, really. When you say [super doSomething], the compiler will emit a call to objc_msgSendSuper() instead of objc_msgSend(). Most of the time -- there are some special cases.

一般来说,你应该将 super 视为只有方法调用的目标。它永远不应该被存储在任何地方,并且永远不应被视为任何的目标表达式用于消息传递。

In general, you should treat super as only a target for method calls. It should never be stored anywhere and should never be considered anything but that target expression for messaging.

事实上,使用<$ c $涉及存储的超级可能会被编译器标记。

In fact, uses of super that involve storage should likely be flagged by the compiler.

就你的错误而言,这听起来很糟糕内存损坏,过度释放和/或并发。您需要提供更多与枚举相关的代码和其他相关代码以进一步推断。

In terms of your bug, that sounds an awful lot like there is either corruption of memory, an over-release, and/or concurrency going on. You'll need to provide more code related to the enumeration and other relevant code to deduce further.

这篇关于什么是“超级”在Objective-C? (自我!=超级)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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