gpt4 book ai didi

objective-c - @property 问题 - Objective C

转载 作者:行者123 更新时间:2023-12-03 17:07:35 24 4
gpt4 key购买 nike

我遇到属性问题。首先,我定义它:

@property (readwrite) BOOL isPerformingOperation;

然后综合它:

@synthesize isPerformingOperation;

然后我将属性设置为:

self.isPerformingOperation = YES;

为了确保我做的事情是正确的,我记录:

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

...按预期返回 1。

但是我需要从另一个类 - DAUpdatingView 读取属性,因此我从添加属性的类导入头文件,并尝试两种获取值的方法,这两种方法总是返回 0,即使我在原始类中将其设置为 1。

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

这是控制台输出:

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

如您所见,属性所在的主类已按预期更改了值,但另一个类没有读取它。我错过了什么?

编辑:

是的,我正在使用原始类的共享实例:

static USBBackupAppDelegate *sharedInstance = nil;

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

最佳答案

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

那不行;每次调用 sharedInstance 时,您都会创建一个新实例(假设 sharedInstance 为 nil)。

当然,除非您在 init 中设置 sharedInstance,这将是一个极其奇怪的模式。

关于objective-c - @property 问题 - Objective C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3917876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com