@synthesized实例变量的可见性是什么? [英] What is the visibility of @synthesized instance variables?

查看:147
本文介绍了@synthesized实例变量的可见性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您在公开界面中有属性,例如以下

If you have a property in your public interface like the following

@interface MyClass : NSObject
@property(strong) NSString *myProp;
@end

然后合成它,实际上合成变量:

And then synthesize it, in effect synthesizing the variable:

@implementation MyClass
@synthesize myProp = _myProp; // or just leave it at the default name..
@end

实例变量 _myProp 的可见性?也就是说,这被认为是 @public @protected @private ?我猜猜,因为 MySubClass 可以继承自 MyClass ,那么它也会获得属性还继承了实例变量的可见性?

What is the visibility of the instance variable _myProp? That is, is this considered @public, @protected or @private? I'm guessing since MySubClass could inherit from MyClass then it would also get the properties (naturally), but would it also inherit the instance variable visibility?

如果我把属性放在类扩展中,会有什么不同?这将隐藏属性从子类,我也猜到实例变量。

What difference does it make if I put the property in a class extension? That would hide the property from subclasses, and I'm guessing the instance variable, too. Is this documented anywhere?

推荐答案

合成的ivar对于所有无法看到的代码都是完全不可见的 @synthesize 行(这基本上意味着.m文件以外的任何内容)。它不是 @protected ,它不是 @private ,它只是未知。使用 @private ivar,其他尝试访问它的代码将被告知它是私有的,但是使用合成的ivar,其他尝试访问它的代码将被告知该字段

A synthesized ivar is completely invisible to all code that cannot see the @synthesize line (which basically means anything outside of the .m file). It's not @protected, it's not @private, it's simply unknown. With a @private ivar, other code trying to access it will be told that it's private, but with a synthesized ivar, other code trying to access it will be told that the field simply doesn't exist.

作为一个思想实验,尝试想象一种情况,其中ivar的行为就像 @protected 。你做一个子类,你在那里的ivar muck。现在你回到超类,并将 @synthesize myProp 更改为 @synthesize myProp = foo 。在子类中会发生什么?当编译器处理子类时,它不能看到 @synthesize 行,因此它不知道你刚刚更改了ivar的名称。事实上,它甚至不能告诉属性是否由一个ivar支持,或者如果它是用自定义的访问器方法实现。我希望很明显为什么这意味着子类不能访问ivar,也不能任何其他类。

As a thought experiment, try imagining a situation where the ivar acted like it was @protected. You make a subclass, and you muck about with the ivar there. Now you go back to the superclass and change @synthesize myProp to @synthesize myProp=foo. What happens in the subclass? When the compiler processes the subclass, it cannot see the @synthesize line, so it would have no idea that you just changed the name of the ivar. In fact, it cannot even tell if the property is backed by an ivar at all, or if it's implemented with custom-written accessor methods. I hope it's obvious why this means that the subclass cannot possibly access the ivar, and neither can any other class.

这就是说,我不知道什么编译器如果你写代码在同一个.m文件,试图访问该ivar。我期望它会将ivar视为 @private (因为编译器实际上可以看到ivar存在)。

That said, I'm not quite sure what the compiler does if you write code in the same .m file that tries to access the ivar. I expect it will treat the ivar as @private (since the compiler can, in fact, see that the ivar exists).

此外,这些对运行时方法没有任何影响。其他类仍然可以使用obj-c运行时方法来动态查找你的类的ivar列表和关于它的muck。

Also, none of this has any bearing on the runtime methods. Other classes can still use the obj-c runtime methods to dynamically look up your class's ivar list and muck about with it.

这篇关于@synthesized实例变量的可见性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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