2012-03-11 121 views
2

以前i posted a question in SO,不知道是什么导致了这个错误。后来我创建了一个新项目并重新创建了错误。这就是它说的;未知类型考试和未知类型AppDelegate错误

我收到一个错误,称为未知类型名称Exam。我有一个问题发布在不同的标题下。但是现在我发现这个问题是什么(所以我创造了另一个问题,希望大家不会反对这一举动,并提供我解决我的问题:))

我创建了一个新的项目,找到了什么问题。我创建了一个NSObject类叫Exam

在AppDelegate.h中我添加了以下内容;

#import <UIKit/UIKit.h> 
#import "Exam.h" 
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>{ 
    Exam *ex; 
} 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) Exam *ex; 
@property (strong, nonatomic) UITabBarController *tabBarController; 

@end 

在AppDelegate.m我只有合成ex,所以我在这里不是粘贴代码。

现在在考试类(NSObject类)中,我有以下代码;

#import "AppDelegate.h" 
#import <Foundation/Foundation.h> 

@interface Exam : NSObject { 
    AppDelegate*APP; <-- here i get Unknown type AppDelegate. 
} 

@end 

,只要我在这里创建了AppDelegate对象,我得到的错误。

注:我使用ARC

回答

9

变化Exam.h

#import <Foundation/Foundation.h> 

    @class AppDelegate 

    @interface Exam : NSObject { 
     AppDelegate*APP; <-- here i get Unknown type AppDelegate. 
    } 

    @end 

那么你Exam.m上述@implementation做到这一点

#import "AppDelegate.h" 
// Rest is same 
    @implementation ... 

当前正在导入在exam.h中的Appdelgate.h和AppDelegate.h中的Exam.h使这两个类都成立ES执行本身之前导入对方..这使编译器errors..since每个类是指其他..

+0

是的,这工作。我可以知道是什么原因导致了这个错我会在3分钟内将问题标记为“已接受”。谢谢 – Illep 2012-03-11 16:13:48

+1

错误是由于头文件互相引用造成的。导入意味着首先读取头文件,但如果该头文件引用原始文件,编译器会认为它已经读取并且未访问它。这就是为什么最好使用@class。 – 2012-03-11 16:44:12