views:

60

answers:

3

Hello,

I'm having a problem with properties. First, I define it:

@property (readwrite) BOOL isPerformingOperation;

then synthesize it:

@synthesize isPerformingOperation;

Then I am setting the property as such:

self.isPerformingOperation = YES;

To make sure I've done things right, I log:

NSLog(@"class perform is %i",self.isPerformingOperation);

... which returns 1 as it is supposed to.

But then I need to read the property from another class - DAUpdatingView, so I import the header file from the class I added the property to and try two ways of getting the value, which both always return 0, even when I set it in the original class to 1.

NSLog(@"My Boolean: %d, or %@", [USBBackupAppDelegate sharedInstance].isPerformingOperation, [USBBackupAppDelegate sharedInstance].isPerformingOperation ? @"Yes" : @"No");

This is the console output:

2010-10-12 19:32:11.381 USBBackup[3329:a0f] class perform is 1
2010-10-12 19:32:15.330 USBBackup[3329:a0f] My Boolean: 0, or No

As you can see, the main class where the property is from has changed the value as it was supposed to, but the other class doesn't read it. What am I missing?

Edit:

Yes, I am using a shared Instance of the original class:

static USBBackupAppDelegate *sharedInstance = nil;

+ (USBBackupAppDelegate *)sharedInstance
{
    return sharedInstance ? sharedInstance : [[self alloc] init];
}
+1  A: 

properties are per-instance and not shared between objects. Read the answers to this question to see the code that does what you want

http://stackoverflow.com/questions/695980/how-do-i-declare-class-level-properties-in-objective-c

Lou Franco
I have done this - I use +sharedInstance to achieve this. The result is still the same.
David Schiefer
sharedInstance is not the same object as ones you alloc.
Lou Franco
in your sharedInstance message, what is the sharedInstance variable? How is it declared and how does it become non-nil? If you declare as an instance variable in your header, it's not shared between instances. You need to create a local static in the sharedInstance message.
Lou Franco
And if you alloc another object, it's not shared.
Lou Franco
A: 

Well, USBBackupAppDelegate*app = [[USBBackupAppDelegate alloc] init]; creates a new object instance. And in the new instance, the property has not been set to YES yet.

DarkDust
true, the sharedInstance however, which shares the original instance still returns the same. :/
David Schiefer
+1  A: 
+ (USBBackupAppDelegate *)sharedInstance
{
    return sharedInstance ? sharedInstance : [[self alloc] init];
}

That doesn't work; every time you call sharedInstance, you are creating a new instance (assuming sharedInstance is nil).

Unless, of course, you are setting sharedInstance in init, which would be an exceedingly odd pattern.

bbum
sharedInstance is nil.
David Schiefer
I forgot the init implementation of it, hence it didn't work. Now it does, thanks!
David Schiefer
right, and it stays that way, which means that the message always returns a new object, that isn't shared.
Lou Franco
The official Apple-blessed way of writing singletons used to be to set it in `-allocWithZone:`. Why? Well, I guess you’d know better than me. :-)
Ahruman