unknown type name in objective c

31,522

Solution 1

This problem happen to me once.

I was importing the "APPDelegate.h" in my h file and in my APPDelegate.h I was importing the file too (it shouldn't be a problem but...)

What I did: I changed the Import from my own .h to .m and it worked :)

Solution 2

I figured out, that the same error appears if you have an import-cycle:

Class_A.h: #import "Class_B.h"

Class_B.h: #import "Class_A.h"

To fix: look for any imports of the offending class (the error tab is your friend, expand the relevant error for a list of imports). Remove #import's accordingly

Solution 3

As others have mentioned, this is indeed caused by cyclic imports. To fix this remove the imports in one of the classes. But sometimes this is not sufficient. If the classes depend on each other, simply forward-declare the one class in the other:

Class A:

#import <UIKit/UIKit.h>
@class B; //<- this is essential here

@interface A: NSObject

@property(nonatomic, strong) B *b;
//...

In class B we have:

#import "A.h"
@interface B: NSObject

@property(nonatomic, strong) A *a;

Solution 4

@JustAStranger and @NathanielSymer, both are correct!

Anyway, worth remember that this case, below, has the same problem too:

Class_A.h: #import "Class_B.h"

Class_B.h: #import "Class_C.h"

Class_C.h: #import "Class_A.h"

This problem reveal to us how important is take care about owners at our Class relationships. Is very easy creates cycle problems using ObjC headers.

Share:
31,522
Amit Hagin
Author by

Amit Hagin

Updated on July 09, 2022

Comments

  • Amit Hagin
    Amit Hagin almost 2 years

    I'm pretty new to objective c, and having some basic problems.

    I wrote a simple program using a navigator, and everything worked fine. then I added few lines of code (can't even remember what exactly, and it seems to have no connection to the problem) and the problem occurred. I tried ctrl+z, and the problem remained:

    I run the program and get these errors:

    1. unknown type name "mainController"
    2. property with 'retain (or strong)' attribute must be of object type
    

    while mainController is the first screen to be loaded.

    This is the appDelegate.h file:

    #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
    

    this is the relevant part of the appDelegate.m file:

    - (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;
    }
    

    And this is 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
    

    it already worked...
    can you figure it out?

    thanks

  • jackal
    jackal almost 9 years
    In addition to this answer. In case if you need to have that type in your header but want to avoid import-cycles, just use forward declarations. e.g.: @class mainController;