Jorgen
Jorgen

Reputation: 475

iOS Property not found on object of type 'AppDelegate *'

I have two viewControllers accessing a NSNumber on the AppDelegate. One of them can see it, and the other can't. I am totally confused by this.

The one with the problem has this code.

AppDelegate *dataStore = (AppDelegate *)[[UIApplication sharedApplication] delegate];
dataStore.downHUD = [NSNumber numberWithFloat:(float)progress];

The other has this.

AppDelegate *dataStore = (AppDelegate *)[[UIApplication sharedApplication] delegate];
dataStore.downHUD = [NSNumber numberWithFloat:(float)0];

Both imports the AppDelegate in the .m file but I end up with

Property 'downHUD' not found on object of type 'AppDelegate *'

with the first one. Anyone that can help me see what's wrong?

I copied and pasted a lot of code into the AppDelegate by mistake, that has been corrected. Is there some sort of link that could got broken?

Upvotes: 0

Views: 13954

Answers (4)

Relie Essom
Relie Essom

Reputation: 1374

If you're using expo this is how to get rid of the bug. "reactDelegate" was introduced in sdk 44.0.0 so if you're using sdk 43.0.0 here's is how your code should look in your "AppDelegate.m" from line 35 - 46.

RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"main" initialProperties:nil];
rootView.backgroundColor = [UIColor whiteColor];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];

[super application:application didFinishLaunchingWithOptions:launchOptions];

Upvotes: 1

SteveH
SteveH

Reputation: 11

Your two view controllers may refer to different AppDelegate code. Even though the Xcode Project Navigator shows only one set of AppDelegate files, and Jump to Definition shows the same AppDelegate class definition in both cases, one of the view controllers may actually have different delegate code.

I had this very problem with a delegate class definition, where some member variables were only available in one view controller but not in the other.

Right-click on each ViewController.m file in the Project Navigator, and use Show in Finder to see whether they are both in the same location as the desired AppDelegate files. If not, move the VC files to the correct location and add them to the project.

Upvotes: 1

Lorenzo B
Lorenzo B

Reputation: 33428

Maybe there is no such property in your AppDelegate class.

In your AppDelegate.h under interface declaration you need to have

@property (nonatomic, retain) NSNumber* downHUD;

In your AppDelegate.m under implementation declaration you need to have

@synthesize downHUD;

In this manner you define accessors (getter and setter) to access an instance variable called downHUD. This accessors are public and you can do

dataStore.downHUD = ...

Maybe this could be the error. But without AppDelegate code it's difficult to understand what is going on.

Hope it helps.

Edit:

It's no a good strategy to access data within the application delegate. I suggest you to use singletons like singletons-appdelegates-and-top-level.html

Edit 2:

@interface SingletonModel : NSObject {
    NSNumber* downHUD_;
}

+ (id)sharedInstance;

@property (nonatomic, retain) NSNumber* downHUD;

@end

#import "SingletonModel.h"

@implementation SingletonModel

@synthesize downHUD = downHUD_;

static SingletonModel *sharedInstance = nil;

+ (SingletonModel *)sharedInstance {
    if (sharedInstance == nil) {
        sharedInstance = [[super allocWithZone:NULL] init];
    }

    return sharedInstance;
}

- (id)init
{
    self = [super init];

    if (self) {

    }

    return self;
}

-(void)dealloc
{
    [super dealloc];
}

+ (id)allocWithZone:(NSZone*)zone {
    return [[self sharedInstance] retain];
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

- (id)retain {
    return self;
}

- (NSUInteger)retainCount {
    return NSUIntegerMax;
}

- (oneway void)release {

}

- (id)autorelease {
    return self;
}

@end

To set your model:

SingletonModel* model = [SingletonModel sharedInstance];
model.downHUD = ...

To read your model:

SingletonModel* model = [SingletonModel sharedInstance];
NSNumber* n = model.downHUD;

For other info read iphone-code-snippet-the-singleton-pattern and singleton-classes. About Singletons you can find in apple documentation at Cocoa Fundamentals Guide and at Singleton.

Upvotes: 8

Pascal
Pascal

Reputation: 16941

If you have imported the header files then it should work. Did you try to clean and re-build your project? You can do that with CMD + Shift + K (or by selecting Clean from the Project menu).

Upvotes: 0

Related Questions