How “Unknown class <MyClass> in Interface Builder file” error can be fixed with a line reads "[MyClass class]"?
Asked Answered
M

2

0

I read the following answer, so I know that "Unknown class in Interface Builder file" error can be solved using the -ObjC linker option. (FYI, MyClass is in static library.)

https://mcmap.net/q/65135/-quot-unknown-class-lt-myclass-gt-in-interface-builder-file-quot-error-at-runtime

I've also found that single line of [MyClass class] code in the App Delegate can handle the same problem without using -ObjC option. My Question is how come the code can work.

According to the answer that I've attached above, the error occurs because symbols in my static libraries are not loaded. Then, it means [MyClass class] makes the Linker to load the symbol at runtime? (which doesn't make sense)

Macedonian answered 9/11, 2013 at 9:56 Comment(0)
H
2

The linker will try to reduce the final binary size by removing unused symbols. The classes instantiated from Storyboard are created using reflection, and thus it's impossible to the linker to know if that class is being used, so it's removed from the final binary.

By adding that line to your code you are forcing the linker to add that class at compilation time (not at runtime), making the reflection call from Storyboard to work as expected.

Holbrook answered 9/11, 2013 at 10:2 Comment(1)
thanks. not to confuse, i've added a sentence that hints the linker is not related to runtime.Macedonian
S
1

I also encountered this problem. The accepted answer did not solve my answer. Here is what solved my answer, and what I think is the actual way that it should be done:

In IB, where you can select your class, there is another drop-down menu right below where you enter the name of your class, called 'Module'. Here you can select either nothing (for classes in Objective-C), or you can select a pre-filled module, usually the name of your app. If you do this, the runtime understands that this class runs in a Swift-module named and is able to load it.

This works for a project that started out as Obj-C only, and had some swift-classes added later.

Spatula answered 13/9, 2014 at 7:22 Comment(1)
Thanks! This was exactly what I needed. IB should be fixed to deal with this problem.Quadrifid

© 2022 - 2024 — McMap. All rights reserved.