Xcode 中的未知类型名称(即使有 @class 声明)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14063317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:24:24  来源:igfitidea点击:

Unknown Type Name in Xcode (even with @class declaration)

objective-ciosxcode

提问by matthewpalmer

I'm having trouble with a simple app setting up a data controller. I get an error on the line @property (strong, nonatomic) BirdsListDataController *dataController;in BirdsListViewController.h. I've tried my best to use a @class declaration of BirdsListDataController, as well as trying to remove any #import statements from the .h files and tried to remove a circular #import which you can find commented out in the top of BirdsListViewController.h. I'm guessing it's something simple.

我在设置数据控制器的简单应用程序中遇到了问题。我@property (strong, nonatomic) BirdsListDataController *dataController;在 BirdsListViewController.h 中收到一条错误消息。我已尽力使用 BirdsListDataController 的 @class 声明,并尝试从 .h 文件中删除任何 #import 语句,并尝试删除循环 #import,您可以在 BirdsListViewController 顶部找到注释掉的循环。 H。我猜这很简单。

BirdsListViewController.h

BirdsListViewController.h

#import <UIKit/UIKit.h>
@class BirdsListDataController;

@interface BirdsListViewController : UITableViewController <UITextFieldDelegate>
{
// NSMutableArray *listOfBirds;
IBOutlet UITextField *addNewBirdTextField;

}
//@property (nonatomic, retain) NSIndexPath *checkedIndexPath;
@property (nonatomic, retain) NSString *textLabelContents;
@property (nonatomic, retain) NSMutableArray *workingArray;
@property (strong, nonatomic) BirdsListDataController *dataController;
@property (strong, nonatomic) IBOutlet UITableView *birdListTableView;

@end

BirdsListViewController.m

BirdsListViewController.m

#import "BirdsListViewController.h"
#import "BirdsListDataController.h"

@interface BirdsListViewController ()
@end

@implementation BirdsListViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
...

BirdsListDataController.h

BirdsListDataController.h

#import <Foundation/Foundation.h>     
@class BirdName;

@interface BirdsListDataController : NSObject

@property (nonatomic, copy) NSMutableArray *listOfBirds;
-(NSUInteger)countOfList;
-(BirdName *)objectInListAtIndex:(NSUInteger)theIndex;
-(void)addBirdNameWithName:(BirdName *)bName;
@end

BirdsListDataController.m

BirdsListDataController.m

#import "BirdsListDataController.h"
//#import "BirdsListViewController.h"
#import "Bird.h"

@implementation BirdsListDataController

-(id)init
{...

I'm still really new to iOS and Objective C, so hopefully my code isn't too awful to troubleshoot. Thanks for the help.

我对 iOS 和 Objective C 还是很陌生,所以希望我的代码不会太糟糕而无法进行故障排除。谢谢您的帮助。

采纳答案by Darren

I'm not certain what is causing your problem, but a few things:

我不确定是什么导致了您的问题,但有几点:

  • In the code that you've presented there is no reason not to import BirdListDataController.h in BirdListViewController.h, since there is no reference to BirdListViewControllers in BirdListDataController.h. So try replacing your @class declaration with an #import statement.

  • In BirdListDataController.h you declare @class BirdName, but in BirdListDataController.m you import Bird.h instead of BirdName.h. It seems like something could be wrong there, although I would have to see the code for BirdName.h and Bird.h to know for sure.

  • 在您提供的代码中,没有理由不在 BirdListViewController.h 中导入 BirdListDataController.h,因为在 BirdListDataController.h 中没有对 BirdListViewControllers 的引用。所以尝试用#import 语句替换你的@class 声明。

  • 在BirdListDataController.h 中声明@class BirdName,但在BirdListDataController.m 中导入Bird.h 而不是BirdName.h。虽然我必须看到 BirdName.h 和 Bird.h 的代码才能确定,但​​那里似乎有些问题。

回答by btomw

For people looking for a better answer than comment/uncomment your code, a better solution is to clean your project and to delete your derived data. Once you've fixed your circular references, the keystroke Command+Shift+Kwill clean your project, or you can go to and select Product->Clean.

对于寻找比注释/取消注释代码更好的答案的人来说,更好的解决方案是清理您的项目并删除您的派生数据。修复循环引用后,击键Command+Shift+K将清理您的项目,或者您可以转到并选择Product->Clean

To delete your derived data, open Organizer, click on the Projects tab, navigate to your project in the sidebar. You should see "Derived Data" under the project name header. To the right of that should be a button saying delete. If it is enabled, deleting the derived data can also remove hanging errors.

要删除您的派生数据,请打开管理器,单击“项目”选项卡,在侧栏中导航到您的项目。您应该在项目名称标题下看到“派生数据”。在它的右边应该是一个说删除的按钮。如果启用,删除派生数据也可以消除挂起错误。

As way of explanation, it seems sometimes that Xcode becomes out of sync with a project, holding on to errors that no longer exists. This is better in more recent version, but still happens occasionally.

作为解释,有时 Xcode 似乎与项目不同步,保留不再存在的错误。这在较新的版本中更好,但仍然偶尔发生。

回答by carlos16196

In my case I had duplicate class names in different folders structure, Once I removed the new class and named it differently everything worked again.

在我的情况下,我在不同的文件夹结构中有重复的类名,一旦我删除了新类并以不同的方式命名,一切都会再次运行。

So to translate this into a practical solution, as per "shA.t"'s comment:

因此,根据“shA.t”的评论,将其转化为实用的解决方案:

if you comment/uncomment your code, or clean project as above answers suggested but still doesnt solve it:

如果您注释/取消注释您的代码,或者按照上述答案建议清理项目,但仍然没有解决它:

  1. take a step to look back at recent classes changes and double check all class names are unique even if in different directories, double check

    them all

  2. if duplicate class name found: make a backup of that code, delete that class (not just reference, but to trash too)
  3. create a new class with unique name and incorporate the backed up code
  1. 采取一步回顾最近的类更改并仔细检查所有类名称是否唯一,即使在不同的目录中,仔细检查

    商场

  2. 如果发现重复的类名:备份该代码,删除该类(不仅是引用,还要删除)
  3. 创建一个具有唯一名称的新类并合并备份代码

For this particular duplicate class name scenario, this will save you the hassle of importing and commenting your #import "class.h"

对于这种特殊的重复类名场景,这将省去您导入和注释 #import "class.h" 的麻烦