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

查看:16
本文介绍了@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 混为一谈.现在返回超类并将@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.

也就是说,如果您在尝试访问 ivar 的同一个 .m 文件中编写代码,我不太确定编译器会做什么.我希望它将 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 列表并对其进行处理.

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天全站免登陆