English 中文(简体)
"Unknown class <MyClass> in Interface Builder file" error at runtime
原标题:

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

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

最佳回答

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.

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.

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.

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.

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];

does the trick (as long as you derive from an 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 MyClass is used somewhere so that it isn t so aggressive in optimizing it out.

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.

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

问题回答

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 Inspector to the right, with that .m-file info)

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

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.

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 -ObjC flags to the Other Linker Flags key the Project (and possibly the Target) Build Settings.

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_load linker flag. If you skip these flags sooner or later you will run into the error of unrecognized selector or get other exceptions such as the one you ve observed here.

I did run into this problem today using Swift.

I changed a class Model.h + Model.m to a Model.swift. This object was used in Interface Builder with the 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:

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"

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" ,

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

It s a Xcode4 cache problem, just delete all folders under /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...

Good luck. Pascual

Sometimes IBuilder missed 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">

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

enter image description here

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

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

customClass="UnrecognizedClassName"

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

worked for me.

enter image description here

I just want to add this answer since most if not all the answers here assume that the class actually exists.. it s just that the linker/compiler is too dumb to see it.. thus the answers revolve around either alerting the linker to the existence of the class or creating a hack to force exist it..

my problem happens when this message is actually talking about a non-existent class.. so an example would be me reverting back to an old git revision that has no knowledge of a certain class.. yet the compiler complains that the said class doesn t exist..

solution?

  • Nuke the whole thing! first delete all the build files etc by deleting all the contents in this directory ~/Library/Developer/Xcode/DerivedData
  • delete the app from the phone itself (and clear the simulator contents if you are using a simulator)

you should be good to go after that

The best way to remove the error is: 1) Select the Class File (.m) 2) Under "Target Membership", "check" the Project name entry

I fixed this by copying the text from my class.h and .m, deleting those class files from the project, and creating new class.h and .m files with the same name using "Add File". Then I pasted the code back into the new files, and everything worked great. Somehow the files weren t linked correctly when they were created. I didn t need to use any linker flags after that.

I FINALLY fixed this, I had forgotten to add the following code to my .m file:

@implementation MyTableViewCell

@end

So it was being caused because I had made a placeholder @interface for my table cell, that had a connection to an element in the .xib file, but there is a bug in Interface Builder where if no @implementation is specified for a class, it can t find it.

I had gone through all of the steps from other forums of viewing the .xib as source and seeing MyTableViewCell even though I had commented it out of my code. I had tried resetting the simulator. I even tried breaking all of my classes up into separate files named the same as the interfaces, but nothing worked until this.

P.S. in my experience, it doesn t matter if the names of the .h/.m files are different from the names of the @interface. I have several files containing more than one @interface and they work fine.

P.P.S. I have a more detailed explanation of why UITableViewCell and UICollectionViewCell cause this error at https://stackoverflow.com/a/22797318/539149 along with how to reveal it at compile-time using registerClass: forCellWithReuseIdentifier:.

This happens because the .xib has got a stale link to the old App Delegate which does not exist anymore. I fixed it like thus:

  • Right click on the .xib and select Open as > Source code
  • In this file, search the old App delegate and replace it with the new one

just add below code at the starting of appdelegate applicatoindidfinishlanching method,then it will works fine

[myclass class];

In my case, I have XCode6, the specified class .m file end up in the wrong place in build phase - It should have been under Compile Sources, but end up in the Copy Bundle Resources

I tried this, and other, answers listed on this site, none of which sorted it for me. This comments (from http://www.iphonedevsdk.com/forum/iphone-sdk-development/43330-unknown-class-interface-builder-file.html) helped:

After searching and searching and searching, I finally discovered the name of this deleted class hidden in a file. I had to open the interface builder files in X-code, by right clicking on them and choosing view as source code . Then searching for it came up with

<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>com.apple.InterfaceBuilder.IBCocoaTouchP lu gin</string>
<string>*this was the class name*</string>

Simply removing that last line doesn t fix it unfortunately, complaining that there are the wrong number of items in the file. You need to remove the corresponding line in the section of lines above it, which refers to CustomClass.

Not only in project settings, but in Target setting also u have to add -all_load -ObjC flags..

Core-Plot: Unknown class CPLayerHostingView in Interface Builder file

This problem does not seem to get outdated.

I had the same issue with Xcode 8 and solved it similiar like smilebot:

  1. Open your storyboard file as "Source code" within Xcode:

  2. Search for the class being referred to & remove the whole bit that says

customClass="UnrecognizedClassName"

  1. Open your storyboard file as "interfacebuilder - storyboard" again and rebuild your app.

Just remove the MyClass.m and .h and add them to project again is work for me.

I had Unknown class favouritesButton in Interface Builder file and tracked it down to a storybook scene where the button in question had a bogus custom class of "favouritesButton" in the Class field at the top of the Identity Inspector. I meant to put that value in the next field: Identity Label.

Changing this to "UIButton" solved the problem.

I ran into this in Swift.

Moving the .xib file into the project s Base.lproj folder got rid of this error.

I had this error crop up today when converting my aaLuminate app to Universal under Xcode 4. This app is based on the utility template and was originally built under Xcode 3.

To save time I copied the iPhone Main and Flipside Views across to appropriate names on the Universal app. I experienced the "Unknown class x in Interface Builder file" error. In my case it was nothing in the XIB files or targets.

I had also copied the aaLuminate-Info.plist file across for other reasons - this had an old key "Main nib file base name" set to MainWindow.

As soon as I deleted this key it fixed the problem!

In my case I got this error because I d tried to save some work by creating a new project and then deleting several of the source files and copying over the source files of the same name from the working project. I also copied my MainStoryBoard file which was looking for my RootViewController. However, when I had deleted the original RootViewController and then added in the RootViewController from the previous product, evidently the Add Files operation failed to "check" the target box as suggested above. By merely visting all of the newley imported ".m" files and making sure that the target membership box was checked, all was well. I think what was happening was that the storyboard file was looking for a class that had been "excluded" from the link because the target membership was unchecked. Making sure the required files for the target are so designated in the target membership in the file inspector did the trick. Thanks Pat! (see above)

In my case it was because I declared a subclass of a subclass of a UITableView cell in the .h file (the declaration of both subclasses were in the same .h file), but forgot to make an empty implementation of that second subclass in the .m file.

don t forget to implement any subclass of a subclass you declare in the .h file! sounds simple, but easy to forget because Xcode will do this for you if you are working with one class per .h/.m file.

I had "Unknown class RateView in Interface Builder" where RateView was a subclass of UIView. I had dropped a UIView onto my Storyboard scene and changed the Custom class field to RateView. Still, this error appeared.

To debug, I changed the name of my class to RateView2 and changed all references to match except the Custom class field of the UIView. The error message still appeared as before with RateView as the missing class. This confirmed that the error message was related to the value of the Custom class field. I changed this value to RateView2 and the error message changed to "Unknown class RateView2 in Interface Builder". Progress of sorts.

Finally, I inspected the source code files themselves in the File Inspector. There I discovered that the source code file (which I had copied from a tutorial) was not associated with my Target. In other words, it had no Target Membership. I checked the box that made the class s source code file a member of the target app and the error message went away.

In my case I had deleted a class called "viewController" not realising it was selected with the storyboard s identity inspector (under Custom Class up the top).

You just have to simply select the correct class for the view controller in your identity inspector s Custom Class field or add a new class to your project and select that one as your Custom Class.

Worked for me!

I added the file Under Build Phase in Targets and the issue got resolved. For the steps to add the file, see my answer at:

Apple Mach-O Linker error (APActivityIcon)

This drove me nuts for a bit and none of the suggestions above helped me get rid of the error. Luckily I only had one IB object using the class so I just deleted it and added it back with the same class specified. Error went away...





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签