开发者

@property problem - Objective C

开发者 https://www.devze.com 2023-01-19 14:02 出处:网络
I\'m having a problem with properties. First, I define it: @property (readwrite) BOOL isPerformingOperation;

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]开发者_Go百科 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];
}


+ (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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消