xcode 运行时出现“接口生成器文件中的未知类 <MyClass>”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1725881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:12:31  来源:igfitidea点击:

"Unknown class <MyClass> in Interface Builder file" error at runtime

cocoacocoa-touchxcodeinterface-builder

提问by jhoule

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

尽管 Interface Builder 知道 a MyClass,但我在启动应用程序时收到错误消息。

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

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

采纳答案by jhoule

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.

尽管在运行时打印了“ Unknown class MyClass in Interface Builder file.”错误,但这个问题与 Interface Builder 无关,而是与链接器有关,链接器没有链接类,因为没有代码直接使用它。

When the .nib data (compiled from the .xib) is loaded at runtime, MyClassis 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.

当 .nib 数据(从 .xib 编译)在运行时加载时,MyClass使用字符串引用,但链接器不分析代码功能,只分析代码存在,所以它不知道。由于没有其他源文件引用该类,链接器在生成可执行文件时将其优化为不存在。所以当 Apple 的代码试图加载这样一个类时,它找不到与之关联的代码,并打印警告。

By default, Objective-C targets will have -all_load -ObjCflags 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.

默认情况下,Objective-C 目标将-all_load -ObjC默认设置标志,这将保留所有符号。但是我从 C++ 目标开始,并没有。尽管如此,我找到了解决此问题的方法,使链接器保持攻击性。

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

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

+(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.

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

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

正如 jlstrecker 在评论中指出的那样,我们实际上并不需要添加_keepAtLinkTime方法。只需调用现有的,例如:

   [MyClass class];

does the trick (as long as you derive from an NSObject).

可以解决问题(只要您从 派生NSObject)。

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 MyClassis used somewhere so that it isn't so aggressive in optimizing it out.

当然,您可以在代码的任何位置调用它。我想它甚至可能在无法访问的代码中。这个想法是让链接器认为MyClass在某处使用了它,这样它就不会那么积极地优化它。

Xcode 6.3.2 & Swift 1.2

Xcode 6.3.2 和 Swift 1.2

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.

视图的快速定义。一定要覆盖init(coder aDecoder: NSCoder). 视图控制器的 Objective-C 定义。还有,梨树上的一个笔尖。

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

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

回答by Pat

I fixed this along the lines of what Laura suggested but I didn't need to recreate the files.

我按照劳拉的建议修复了这个问题,但我不需要重新创建文件。

  • Using XCode 4, in the Project Navigator, select the .m file that contains the class that it is complaining about

  • Go to View->Utilities->Show File Inspector
    (this will show the File Inspectorto the right, with that .m-file info)

  • Open the Target Membershipsection and make sure that your target is selected for this .m-file

  • 使用 XCode 4,在Project Navigator 中,选择包含它所抱怨的类的 .m 文件

  • 转到查看-> 实用程序-> 显示文件检查器
    (这将在右侧显示文件检查器,其中包含 .m 文件信息)

  • 打开Target Membership部分并确保为此 .m 文件选择了您的目标

When I added my .m file to my project, it didn't add it to my default target for some reason and that caused me to get the error you mentioned.

当我将我的 .m 文件添加到我的项目时,由于某种原因它没有将它添加到我的默认目标,这导致我收到您提到的错误。

回答by Alasdair Allan

This doesn't really have anything to do with Interface Builder, what's happening here is the symbols aren't being loaded from your static library by Xcode. To resolve this problem you need to add the -all_load -ObjCflags to the Other Linker Flagskey the Project (and possibly the Target) Build Settings.

这与 Interface Builder 没有任何关系,这里发生的事情是 Xcode 没有从您的静态库中加载符号。要解决此问题,您需要将-all_load -ObjC标志添加到Other Linker Flags项目(可能还有目标)构建设置的键中。

Since Objective-C only generates one symbol per class we must force the linker to load the members of the class too by using the -ObjC flag, and we must also force inclusion of all our objects from our static library by adding the -all_loadlinker flag. If you skip these flags sooner or later you will run into the error of unrecognized selectoror get other exceptions such as the one you've observed here.

由于 Objective-C 只为每个类生成一个符号,我们必须使用 -ObjC 标志强制链接器也加载类的成员,并且我们还必须通过添加-all_load链接器标志来强制包含我们静态库中的所有对象。如果您迟早跳过这些标志,您将遇到错误unrecognized selector或获得其他异常,例如您在此处观察到的异常。

回答by Besi

I did run into this problem today using Swift.

我今天使用 Swift 确实遇到了这个问题。

I changed a class Model.h + Model.mto a Model.swift. This object was used in Interface Builder with the class = Model.

我将一个类Model.h + Model.m更改为Model.swift. 这个对象在 Interface Builder 中使用class = Model

As soon as I replaced the object the class could no longer be loaded.

一旦我替换了该对象,就无法再加载该类。

What I had to do was to change the class reference in IB from:

我必须做的是将 IB 中的类引用从:

Class = Model
Module = 

to

Class = Model
Module = <TARGETNAME>

You'll find the <TARGETNAME>in the build settings. It is also the name that shows up in your generated Swift-Header: #import "TARGETNAME-Swift.h"

您将<TARGETNAME>在构建设置中找到。它也是您生成的 Swift-Header 中显示的名称:#import "TARGETNAME-Swift.h"

回答by Subbu

Go to the "ProjectName" , click on it , and then go the "Build phases" tab , and then click on the "compile sources" , and then click on "+" button , a window will appear , the choose "MyClass.m" file and then click "add" ,

转到“项目名称”,单击它,然后转到“构建阶段”选项卡,然后单击“编译源”,然后单击“+”按钮,将出现一个窗口,选择“MyClass.js”。 m”文件,然后单击“添加”,

Build the Project and Run it , the problem will surely get solved out

Build the Project并运行它,问题肯定会得到解决

回答by Ignacio Pascual

It's a Xcode4 cache problem, just delete all folders under /Users/your_user/Library/Application Support/iPhone Simulator/4.3/Applications/

这是一个Xcode4缓存问题,只需删除/Users/your_user/Library/Application Support/iPhone Simulator/4.3/Applications/下的所有文件夹

Also if you have the same issue testing on your iPhone, delete the old app before running it...

此外,如果您在 iPhone 上测试时遇到同样的问题,请在运行之前删除旧应用...

Good luck. Pascual

祝你好运。帕斯卡尔

回答by ChikabuZ

Sometimes IBuilder missed customModule="AppName" customModuleProvider="target"

有时 IBuilder 会错过 customModule="AppName" customModuleProvider="target"

To fix it, open storyboard as source code and replace this line:

要修复它,请打开故事板作为源代码并替换以下行:

<viewController storyboardIdentifier="StoryboardId" id="SomeID" customClass="CustomClass"
sceneMemberID="viewController">

to this:

对此:

<viewController storyboardIdentifier="StoryboardId" id="SomeID" customClass="CustomClass"
 customModule="AppName" customModuleProvider="target" sceneMemberID="viewController">

回答by Dhilip

My case - Trying to use a Class from a swift framework in my objective c project, I got this error. Solution was to add Module (swift framework) of the class in Interface builder/ Storyboard as shown below. Nothing else

我的情况 - 尝试在我的目标 c 项目中使用 swift 框架中的类,我收到此错误。解决方案是在界面构建器/故事板中添加类的模块(swift 框架),如下所示。没有其他的

enter image description here

在此处输入图片说明

回答by smileBot

In my case it was showing an error for a class that didn't even exist! I suspected it was something that got borked in the storyboard file. If you don't recognize the class file in the error try this:

在我的情况下,它显示了一个甚至不存在的类的错误!我怀疑这是故事板文件中的某些东西。如果您无法识别错误中的类文件,请尝试以下操作:

1) open your project in sublime or another good editor. Search for the class being referred to. 2) remove the whole bit that says

1) 在 sublime 或其他好的编辑器中打开你的项目。搜索被引用的类。2)删除整个位说

customClass="UnrecognizedClassName"

3) save it. 4) return to xcode and clean the project, and try running it now.

3)保存。4) 返回 xcode 并清理项目,现在尝试运行它。

worked for me.

对我来说有效。

enter image description here

在此处输入图片说明

回答by Hymanie Lee

Go to Build Phases-> Compile Sources and add your new .m files.

转到 Build Phases-> Compile Sources 并添加新的 .m 文件。