简体   繁体   中英

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

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.

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

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

@interface BirdsListViewController ()
@end

@implementation BirdsListViewController

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

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

#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.

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+K will clean your project, or you can go to and select 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.

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.

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM