"未知类<MyClass>在界面生成器文件中"运行时错误 [英] "Unknown class <MyClass> in Interface Builder file" error at runtime

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

问题描述

即使 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.

推荐答案

尽管在运行时打印了Unknown class MyClass in Interface Builder file."错误,但此问题与 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 编译)时,使用字符串引用 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 派生).

does the trick (as long as you derive from an 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.

视图的快速定义.一定要覆盖 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.

这篇关于"未知类<MyClass>在界面生成器文件中"运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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