Property 'x' not found on object of type &

2019-01-29 09:43发布

问题:

This error very strange. I have a bunch of properties in app delegate which I have no problem to access. My code worked fine without ARC, but when I turned on ARC I receive this very strange error. Property 'navigationController' not found on object of type 'AppDelegate *'

A bunch of other properties work just fine except this.

I already clean everything and restarted Xcode.

Here is the code:

AppDelegate.h:

@interface AppDelegate : UIResponder <UINavigationControllerDelegate> {
    UINavigationController *navigationController;
    NSString *appName;
}
@property (strong, nonatomic) NSString *appName;
@property (strong, nonatomic) UINavigationController *navigationController;

AppDelegate.m

    #import "AppDelegate.h"
    @synthesize navigationController, appName;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]];
        navigationController = [[UINavigationController alloc] initWithRootViewController:self.startViewController];
}

MyClass.h

@class AppDelegate;
@interface MyClass: UIResponder {
    AppDelegate *appDelegate;

}

MyClass.m

#import "AppDelegate.h"
#import "MyClass.h"

@implementation MyClass 
- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here. 
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
        [self initFacebook];
    }
    return self;
}

- (void) doStuff {
    NSLog(@"App Name:%@",appDelegate.appName); //this works fine

    //This one doesn't
    UINavigationController *myNavigationController = appDelegate.navigationController;
}

Update: I did a workaround. Basically I created a property in MyClass called navigationViewController and I pass the object after MyClass is instantiated in AppDelegate instead of getting directly from AppDelegate in MyCLass (as shown above). I'm still puzzled, it must be a bug in the compiler. I'm still very interested in how to make the original code work.

回答1:

In MyClass.m, you are importing "MyClass" instead of "MyClass.h". Could this be the problem?

Sometimes cleaning a project is not sufficient: you may need to open the Organizer window, select your project under the Projects tab, and click the button to delete Derived Data.



回答2:

OK, I put only the problematic code into a empty project as seen in the question. It compiles fine. It's really puzzling so I concluded it must be a bug in Xcode, or maybe some settings. Anyways I couldn't figure out what went wrong so I just pass the problematic object (navigationController) when MyClass is instantiated in AppDelagate. It's fine with one object and thankfully all the other appDelegate properties/selectors can be accessed fine.