iOS:当我将AppDelegate.h包含到另一个头文件中时,什么可能导致Xcode编译器抛出错误?

时间:2012-04-02 12:45:43

标签: objective-c ios compiler-construction import

我正在尝试#import我的“AppDelegate.h”到同一项目的另一个头文件中,以便访问我的iOS项目的AppDelegate方法。

所以,我的头文件看起来像这样:     #import

#import "DataProvider.h"
#import "MyAppDelegate.h"

@interface MyViewController : UIViewController <UITextFieldDelegate, UIAlertViewDelegate> {
...
}

我希望像这样使用我的AppDelegate:

MyAppDelegate* appDelegate = (MyAppDelegate*)[UIApplication sharedApplication].delegate;
[appDelegate doSomething];

但是,只要#import“MyAppDelegate.h”,编译器就会抛出很多(无关的)错误,比如

Cannot find interface declaration for 'MyOtherViewController', superclass of 'MyOtherViewController2' 

问题是:我可以在其他标题中包含我的AppDelegate。我无法弄清楚可能存在的差异。请帮我弄清楚是什么原因引起的!非常感谢!

PS:这种情况发生在GCC以及新的LLVM上。

1 个答案:

答案 0 :(得分:5)

将#import移动到.m文件中。如果您需要.h中的MyAppDelegate符号,请改用@class MyAppDelegate;