objective-c - 这是什么意思? "Writable atomic property ' fileManager' 无法将合成的 setter/getter 与用户定义的 setter/getter 配对”

标签 objective-c ios xcode xcode4

<分区>

Possible Duplicate:
error: writable atomic property cannot pair a synthesized setter/getter with a user defined setter/getter

我有一个头文件,我在其中声明了一个原子属性,如下所示:

@property (retain) FileManager *fileManager;

然后我在实现文件中综合了它:

@synthesize fileManager;

然后我编写自己的延迟初始化访问器:

- (FileManager*)fileManager {
    if (fileManager) {
        return fileManager;
    }

    FileManager *fm = [[FileManager alloc] init];
    self.fileManager = fm;
    [fm release];

    return fileManager;
}

一切都很完美,Xcode 3.2 从来没有提示过。但是 Xcode 4 警告:

Writable atomic property 'fileManager' cannot pair a synthesized setter/getter with a user defined setter/getter

当我做对时,它试图说:“你不能像过去 3 年那样覆盖合成的 getter/setter,即使它工作得很好!”

显然,抑制警告是愚蠢的。这个属性确实需要线程安全,我真的很想要这个惰性初始化。我现在该怎么办?

编辑根据this question and answer on SO ,这里缺少 @synchronize 代码。 我如何手工使这个访问器正确地线程安全?

我尝试将其重写为:

- (FileManager*)fileManager {
    @synchronized(self) {
        if (fileManager) {
            return fileManager;
        }

        FileManager *fm = [[FileManager alloc] init];
        self.fileManager = fm;
        [fm release];


        return fileManager;
    }
}

但是警告并没有消失。因此,当我正确回答另一个问题时,摆脱此警告的唯一方法是要么不自定义合成原子属性访问器,要么完全省略 @synthesize 并全部手动完成:访问器和增变器。当然,这会很糟糕。是否有更好的解决方案?

如果我将它设置为 @dynamic fileManager 而不是 @synthesize fileManager 是什么意思?

编辑: 所以我尝试使用 @dynamic 而不是 @synthesize。这实际上意味着手动实现访问器和修改器。那我为什么还要将它声明为 @dynamic 呢?

最佳答案

如果你声明一个原子的@property——默认——你必须要么使用@dynamic,让编译器@synthesize一切,要么提供setter和getter的实现。

关于objective-c - 这是什么意思? "Writable atomic property ' fileManager' 无法将合成的 setter/getter 与用户定义的 setter/getter 配对”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7784105/

相关文章:

objective-c - 为什么某些 C 数学表达式需要将常量显式标记为 float ?

objective-c - NSNotifications 的开销

ios - 向Realm托管对象添加字符串列表会导致应用在启动时崩溃

ios - 处理具有底部约束的键盘

ios - 继续 : Display stack to second View Controller

html - 从 NSString 中提取一个 html 标签和属性

ios - 仅使用 XIB 开发自定义可滚动标签栏 - Objective-C

iphone - 如何反转 NSMutableArray 中的数据?

iphone - 在 iOS 中从 Facebook 获取用户详细信息

ios - swift 包管理器中的 "warning: no targets to build in package"错误