1

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];
}
Pripyat
  • 2,937
  • 2
  • 35
  • 69
  • Class refers to the type, each runtime instance of the class is called an object. The last sentence of your question should be "the main object where the property was set has the changed value, but the other object has it's default value" -- objects each have their own copy of all properties, so you can see what's wrong once you understand the difference between classes and objects. – Lou Franco Oct 12 '10 at 18:48
  • I have a class & a subclass of NSView - USBBackupDelegate & DAUpdatingView - I want to get the properties from the USBBackupDelegate Class in the DAUpdatingView subclass. – Pripyat Oct 12 '10 at 18:50

3 Answers3

1

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

How do I declare class-level properties in Objective-C?

Community
  • 1
  • 1
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
  • I have done this - I use +sharedInstance to achieve this. The result is still the same. – Pripyat Oct 12 '10 at 18:48
  • sharedInstance is not the same object as ones you alloc. – Lou Franco Oct 12 '10 at 18:52
  • 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 Oct 12 '10 at 18:55
  • 1
    And if you alloc another object, it's not shared. – Lou Franco Oct 12 '10 at 18:56
1
+ (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
  • 162,346
  • 23
  • 271
  • 359
  • I forgot the init implementation of it, hence it didn't work. Now it does, thanks! – Pripyat Oct 12 '10 at 18:58
  • right, and it stays that way, which means that the message always returns a new object, that isn't shared. – Lou Franco Oct 12 '10 at 18:58
  • 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. :-) – Jens Ayton Oct 12 '10 at 22:40
0

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
  • 90,870
  • 19
  • 190
  • 224
  • true, the sharedInstance however, which shares the original instance still returns the same. :/ – Pripyat Oct 12 '10 at 18:47