"未知类< MyClass>在Interface Builder文件“运行时错误 [英] "Unknown class <MyClass> in Interface Builder file" error at runtime

查看:102
本文介绍了"未知类< MyClass>在Interface Builder文件“运行时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使Interface Builder知道一个 MyClass ,我在启动应用程序时出现错误。

Even though Interface Builder is aware of a MyClass, I get an error when starting the application.

MyClass 是库的一部分时,会发生这种情况,如果我直接在应用程序目标中编译类,就不会发生。

This happens when MyClass is part of a library, and does not happen if I compile the class directly in the application target.

推荐答案

尽管在运行时打印了Interface Builder文件中的未知类MyClass,但是这个问题与Interface Builder无关,

Despite the "Unknown class MyClass in Interface Builder file." error printed at runtime, this issue has nothing to do with Interface Builder, but rather with the linker, which is not linking a class because no code uses it directly.

当在运行时加载.nib数据(从.xib编译)时,<$ p c $ c> MyClass 是使用字符串引用的,但是链接器不分析代码功能,只是代码存在,所以它不知道。由于没有其他源文件引用该类,所以链接器在制作可执行文件时会优化它。因此,当Apple的代码尝试加载这样的类时,它找不到与它相关联的代码,并打印警告。

When the .nib data (compiled from the .xib) is loaded at runtime, MyClass is referenced using a string, but the linker doesn't analyze code functionality, just code existence, so it doesn't know that. Since no other source files references that class, the linker optimizes it out of existence when making the executable. So when Apple's code tries to load such a class, it can't find the code associated with it, and prints the warning.

默认情况下,Objective-C目标在默认情况下设置 -all_load -ObjC 标志,这将保留所有符号。但是我开始用C ++目标,没有。

By default, Objective-C targets will have -all_load -ObjC flags set by default, which will keep all of the symbols. But I had started with a C++ target, and didn't have that. Nevertheless, I found a way around this, which keeps the linker aggressive.

我最初使用的hack是添加一个空的静态例程,如:

The hack I was originally using was to add an empty static routine like:

+(void)_keepAtLinkTime;

这不起作用,但我会调用一次,例如:

which does nothing, but that I would call once, such as:

int main( int argc, char** argv )
{
   [MyClass _keepAtLinkTime];
   // Your code.
}

这将强制链接器保留整个类,错误消失。

This would force the linker to keep the whole class, and the error disappears.

正如jlstrecker在评论中指出的,我们不需要添加 _keepAtLinkTime 方法。只需调用一个现有的,如:

As jlstrecker pointed out in the comments, we do not really need to add a _keepAtLinkTime method. Simply calling an existing one, such as:

   [MyClass class];

这样做只要你从 NSObject )。

当然,你可以在代码的任何位置调用它。我想它甚至可能在不可达的代码。这个想法是愚弄链接器认为 MyClass 在某处被使用,所以它不是那么积极优化它。

Of course, you can call this in any location of your code. I guess it could even be in unreachable code. The idea is to fool the linker into thinking that MyClass is used somewhere so that it isn't so aggressive in optimizing it out.

Swift视图定义。一定要覆盖 init(coder aDecoder:NSCoder)。 Objective-C视图控制器的定义。

Swift definition of view. Be sure to override init(coder aDecoder: NSCoder). Objective-C definition of view controller. And, a nib in a pear tree.

将模块名称添加到您选择类的Nib详细信息检查器。

Add Module Name to Nib details inspector where you pick your class.

这篇关于&quot;未知类&lt; MyClass&gt;在Interface Builder文件“运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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