这是什么意思?“可写原子属性‘fileManager’无法将合成的 setter/getter 与用户定义的 setter/getter 配对" [英] What does this mean? "Writable atomic property 'fileManager' cannot pair a synthesized setter/getter with a user defined setter/getter"

查看:65
本文介绍了这是什么意思?“可写原子属性‘fileManager’无法将合成的 setter/getter 与用户定义的 setter/getter 配对"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
错误:可写原子属性不能配对带有用户定义的 setter/getter 的合成 setter/getter

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

I have a header file where I declare an atomic property, like this:

@property (retain) FileManager *fileManager;

然后我在实现文件中合成:

Then I synthesize that in the implementation file:

@synthesize fileManager;

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

And then I write my own accessor for lazy initialization:

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

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

    return fileManager;
}

一切正常,Xcode 3.2 从未抱怨过.但 Xcode 4 警告:

Everything works perfectly and Xcode 3.2 never complained. But Xcode 4 warns:

可写原子属性fileManager"无法配对合成带有用户定义的 setter/getter 的 setter/getter

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

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

When I get that right it is trying to say: "You cannot overwrite synthesized getters/setters like you did the past 3 years, even though it worked perfectly!"

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

Obviously surpressing warnings is stupid. This property really needs to be thread-safe, and I really want this lazy initialization. What am I supposed to do now?

根据 SO 上的这个问题和答案,这里缺少 @synchronize 代码.我如何手动使这个访问器正确地线程安全?

According to this question and answer on SO, the @synchronize code is missing here. How would I make this accessor properly thread-safe by hand?

我尝试将其重写为:

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

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


        return fileManager;
    }
}

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

But the warning doesn't go away. So when I get the answer of that other question right, the only way to get rid of this warning is to either not customize synthesized atomic property accessors or to omit the @synthesize completely and do it all by hand: The accessor AND the mutator. Of course, this would suck. Is there a better solution to the problem?

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

What does it mean if I set it to @dynamic fileManager instead of @synthesize fileManager?

所以我尝试使用 @dynamic 而不是 @synthesize.这真的意味着手动实现访问器和修改器.那我为什么还要声明它为 @dynamic 呢?

So I tried using @dynamic instead of @synthesize. It really means to implement both accessor and mutator manually. Then why would I even want to declare it as @dynamic at all?

推荐答案

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

If you declare an @property that is atomic -- the default -- you must either use @dynamic, let the compiler @synthesize everything or provide an implementation of both the setter and getter.

这篇关于这是什么意思?“可写原子属性‘fileManager’无法将合成的 setter/getter 与用户定义的 setter/getter 配对"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆