unknown type name in objective c

安稳与你 提交于 2019-11-30 17:14:49

This problem happen to me once.

I was importing the "APPDelegate.h" in my h file and in my APPDelegate.h I was importing the file too (it shouldn't be a problem but...)

What I did: I changed the Import from my own .h to .m and it worked :)

JustAStranger

I figured out, that the same error appears if you have an import-cycle:

Class_A.h: #import "Class_B.h"

Class_B.h: #import "Class_A.h"

To fix: look for any imports of the offending class (the error tab is your friend, expand the relevant error for a list of imports). Remove #import's accordingly

As others have mentioned, this is indeed caused by cyclic imports. To fix this remove the imports in one of the classes. But sometimes this is not sufficient. If the classes depend on each other, simply forward-declare the one class in the other:

Class A:

#import <UIKit/UIKit.h>
@class B; //<- this is essential here

@interface A: NSObject

@property(nonatomic, strong) B *b;
//...

In class B we have:

#import "A.h"
@interface B: NSObject

@property(nonatomic, strong) A *a;

@JustAStranger and @NathanielSymer, both are correct!

Anyway, worth remember that this case, below, has the same problem too:

Class_A.h: #import "Class_B.h"

Class_B.h: #import "Class_C.h"

Class_C.h: #import "Class_A.h"

This problem reveal to us how important is take care about owners at our Class relationships. Is very easy creates cycle problems using ObjC headers.

Check the target and the files it is compiling. Perhaps mainController has some how been removed from that target. If so, when building, you would get the message that it cannot be found.

drekka

This problem looks like a typo because class names usually start with an uppercase character. Therefore, mainController could/should be MainController. Check the class name to see if the error is indeed a typo, because the compiler is telling you it cannot find any class called mainController.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!