目标C中的未知类型名称
-
25-10-2019 - |
题
我对客观C非常陌生,并且有一些基本问题。
我使用导航器编写了一个简单的程序,一切都很好。然后,我添加了几行代码(甚至不记得到底是什么,似乎与问题没有任何联系),并且问题发生了。我尝试了Ctrl+Z,问题仍然存在:
我运行该程序并得到这些错误:
1. unknown type name "mainController"
2. property with 'retain (or strong)' attribute must be of object type
而Main Controller是第一个要加载的屏幕。
这是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
这是appdelegate.m文件的相关部分:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
WishesArray = [[WishesList alloc]init];
temp = nil;
[self setViewController:[[mainController alloc]init]];
[self setNavController:[[UINavigationController alloc]initWithRootViewController:self.viewController]];
[self.window setRootViewController:navController];
[self.window makeKeyAndVisible];
return YES;
}
这是maincontroller.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
它已经工作了...
你能弄清楚吗?
谢谢
解决方案
这个问题一次发生在我身上。
我在h文件和appdelegate.h中导入“ appdelegate.h”。
我所做的:我将导入从自己的.h更改为.m,并且奏效了:)
其他提示
我发现,如果您有进口周期,则会出现相同的错误:
class_a.h: #import "Class_B.h"
class_b.h: #import "Class_A.h"
要解决:查找有问题类的任何导入(错误选项卡是您的朋友,请扩展导入列表的相关错误)。消除 #import
因此
正如其他人提到的那样,这确实是由循环进口引起的。要解决此问题,请在其中一个类中删除导入。但是有时候这还不够。如果这些类彼此依赖,则只需彼此转发一个类:
A类:
#import <UIKit/UIKit.h>
@class B; //<- this is essential here
@interface A: NSObject
@property(nonatomic, strong) B *b;
//...
在B类中,我们有:
#import "A.h"
@interface B: NSObject
@property(nonatomic, strong) A *a;
@justastranger和@nathanielsymer,都是正确的!
无论如何,值得记住的是,下面的情况也有同样的问题:
class_a.h: #import "Class_B.h"
class_b.h: #import "Class_C.h"
class_c.h: #import "Class_A.h"
这个问题向我们揭示了在我们的班级关系中关心业主的重要性。使用OBJC标头非常容易地创建周期问题。
检查目标及其正在编译的文件。也许Main Controller有一些如何从该目标中删除。如果是这样,在构建时,您将收到无法找到它的信息。
这个问题看起来像个错字,因为类名称通常从大写字符开始。因此,Main Controller可以/应该是Main Controller。检查类名称以查看错误是否确实是错字,因为编译器告诉您它找不到任何称为MAINCONTROLLER的类。