Objective c 目标c中的未知类型名称
我对目标c很陌生,有一些基本的问题 我用导航器写了一个简单的程序,一切都很好。 然后我添加了几行代码(甚至记不清具体是什么,而且似乎与问题无关),问题就出现了。我尝试了ctrl+z,但问题仍然存在: 我运行程序并得到以下错误:Objective c 目标c中的未知类型名称,objective-c,types,delegates,navigator,Objective C,Types,Delegates,Navigator,我对目标c很陌生,有一些基本的问题 我用导航器写了一个简单的程序,一切都很好。 然后我添加了几行代码(甚至记不清具体是什么,而且似乎与问题无关),问题就出现了。我尝试了ctrl+z,但问题仍然存在: 我运行程序并得到以下错误: 1. unknown type name "mainController" 2. property with 'retain (or strong)' attribute must be of object type 而mainController是要加载的第一个屏幕
1. unknown type name "mainController"
2. property with 'retain (or strong)' attribute must be of object type
而mainController是要加载的第一个屏幕
这是appDelegate.h文件:
#import <UIKit/UIKit.h>
#import "mainController.h"
#import "WishesList.h"
#import "Wish.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) IBOutlet UINavigationController *navController;
@property (strong, nonatomic) IBOutlet mainController *viewController; // this line creates the errors
@property (strong, nonatomic) WishesList *WishesArray;
@property (strong, nonatomic) NSIndexPath *temp;
@end
这是主控制器。h:
#import <UIKit/UIKit.h>
#import "addWishController.h"
#import "displayWish.h"
#import "WishesList.h"
#import "Wish.h"
@interface mainController : UIViewController
@property (nonatomic, weak) WishesList *list;
@property (nonatomic, strong) IBOutlet UITableView *wishTable;
- (void)addWish;
@end
#导入
#导入“addWishController.h”
#导入“displayWish.h”
#导入“WishesList.h”
#导入“Wish.h”
@接口主控制器:UIViewController
@属性(非原子,弱)WishesList*list;
@属性(非原子,强)IBUITableView*wishTable;
-(无效)addWish;
@结束
它已经起作用了…你能猜出来吗
谢谢这个问题看起来像是打字错误,因为类名通常以大写字母开头。因此,mainController可以/应该是mainController。检查类名以查看错误是否确实是输入错误,因为编译器告诉您它找不到任何名为mainController的类。检查目标及其编译的文件。也许mainController已经从该目标中删除了一些内容。如果是这样,在构建时,您将收到无法找到的消息。这个问题曾经发生在我身上 我在我的h文件和我的APPDelegate.h中导入了“APPDelegate.h”,我也在导入该文件(这应该不是问题,但是…)
我所做的:我将导入从我自己的.h更改为.m,并且成功了:)我发现,如果有导入周期,则会出现相同的错误: 类A.h:
#导入“类B.h”
B.h类:#导入“A.h类”
要修复:查找有问题类的任何导入(错误选项卡是您的朋友,展开相关错误以获得导入列表)。删除
#相应地导入@JustAStranger和@NathanielSymer,两者都是正确的
无论如何,请记住,下面这个案例也有同样的问题:
类A.h:#导入“类B.h”
h类:#导入“C.h类”
h类:#导入“A.h类”
这个问题向我们揭示了在我们的阶级关系中照顾主人是多么重要。使用ObjC头很容易产生循环问题。正如其他人所提到的,这确实是由循环导入引起的。要解决此问题,请删除其中一个类中的导入。但有时这还不够。如果类相互依赖,只需在另一个类中向前声明一个类:
A类:
#import <UIKit/UIKit.h>
@class B; //<- this is essential here
@interface A: NSObject
@property(nonatomic, strong) B *b;
//...
请向我们展示mainController.h
。是否定义了一个名为mainController
的类?我添加了mainController.h,因此您也可以在这个答案的附加部分看到它。如果您需要在头中包含该类型,但希望避免导入周期,只需使用前向声明。e、 g.:@class主控制器代码>
#import <UIKit/UIKit.h>
@class B; //<- this is essential here
@interface A: NSObject
@property(nonatomic, strong) B *b;
//...
#import "A.h"
@interface B: NSObject
@property(nonatomic, strong) A *a;