命名超类“消息"时的Objective-C运行时错误 [英] Objective-C-Runtime Bug When Naming a Superclass "Message"

查看:115
本文介绍了命名超类“消息"时的Objective-C运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级等级如下:

@interface Message : NSObject {}
@end

@implementation Message
- (void) dealloc
{
    // I won't be called
    [super dealloc];
}
@end

@interface FooMessage : Message {}
@end

@implementation FooMessage
- (void) dealloc
{
    // should call Message - dealloc
    [super dealloc];
}
@end

以及以下单元测试:

- (void) test
{
    FooMessage* msg = [[FooMessage alloc] init];
    [msg release];
}

测试将始终失败,并显示EXC_BAD_INSTRUCTION. FooMessagedealloc中将其称为超类析构函数,但调用从未到达那里.而是,Objective-C运行时将调用解析到其他位置:

The test will always fail with EXC_BAD_INSTRUCTION. FooMessage calls it's super class destructor in dealloc, but the call never arrives there. Instead, the Objective-C runtime resolves the call to a different location:

如果将Message基类重命名为其他名称,则不会发生该错误. AbstractMessage.似乎还有一个名为Message的类,其定义未公开.

The error doesn't occur if the Message base class is renamed to something else, e.g. AbstractMessage. It appears there is another class named Message, whose definition is not publicly available.

这是一个错误吗?这里到底发生了什么?我是否违反任何命名限制(即使我认为编译器也应对此进行警告)?

Is this a bug? What's really happening here? Am I violating any naming restrictions (even though I think the compiler should warn me about that)?.

它是XCode 3.1.为iPhone OS 3.0进行编译.

It's XCode 3.1. compiling for iPhone OS 3.0.

推荐答案

Objectve-C缺少名称空间的概念.这个问题是众所周知的,通常可以通过使用前缀命名空间来解决(例如 NS Object或 MK MapView").您可以将消息类命名为JrMessage,以避免与名为Message的(未记录)类发生冲突.

Objectve-C lacks the concept of namespaces. The problem is well known and usually worked-around by using prefix-namespaces (like NSObject or MKMapView). You could name your message class JrMessage to avoid the clash with the (undocumented) class named Message.

仅当编译器知道其他类时,它才会警告您.对于私有的,未记录的类,通常不是这种情况.解决此问题的最佳方法是通过在每个类上使用前缀来避免冲突.当Apple将类添加到新版本的OS中时,这也有助于防止将来发生冲突(编译器显然无法警告).

The compiler could only warn you if it knew about the other class. In the case of private, undocumented classes, this often is not the case. The best way to handle this is to avoid clashes by using the prefix on every class. This also helps against future clashes, when Apple adds classes to a new release of the OS (which the compiler obviously couldn't warn about).

进一步的调查显示,至少在iPhone Simulator上,竞争类源自名为"MIME.framework"的私有框架:

Further investigation shows that the competing class origins from a private Framework named "MIME.framework", at least on the iPhone Simulator:

NSLog(@"Message class: %@", [[NSBundle bundleForClass:NSClassFromString(@"Message")] bundlePath]);

... Message class: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.1.3.sdk/System/Library/PrivateFrameworks/MIME.framework

您可能希望将此信息添加到您的错误报告中.

You might want to add this information in your bug report.

这篇关于命名超类“消息"时的Objective-C运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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