Jorgen Jorgen - 5 months ago 30
Objective-C Question

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?

Answer

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.