iOS中四种实例变量的范围类型@private@protected@public@package

文档上记录是这样的

The Scope of Instance Variables

Toenforce the ability of an object to hide its data, the compilerlimits the scope of instance variables—that is, limits theirvisibility within the program. 

为了强制一个对象隐藏其数据,编译器限制实例变量范围以限制其在程序中的可见性

But toprovide flexibility, it also lets you explicitly set the scope atfour levels. Each level is marked by a compilerdirective:

但是为了提供灵活性,苹果也让开发者显式设置范围(四选一)

Directive

Meaning

@private

The instance variable isaccessible only within the class that declares it.

@protected

实例变量只能被声明它的类访问

Theinstance variable is accessible within the class that declares itand within classes that inherit it. All instance variables withoutan explicit scope directive have @protected scope.

实例变量能被声明它的类和子类访问,所有没有显式制定范围的实例变量都是@protected


@public

Theinstance variable is accessible everywhere.

实例变量可以被在任何地方访问。


@package

Using themodern runtime, an @package instance variablehas @public scope inside theexecutable image that implements the class, but acts like@private outside.使用modern运行时,一个@package实例变量在实现这个类的可执行文件镜像中实际上是@public的,但是在外面就是@private【runtime需要再看一下苹果文档Runtime Programming Guide

The @package scope for Objective-Cinstance variables is analogous to private_extern for C variables andfunctions. Any code outside the class implementation’s image thattries to use the instance variable gets a link error.

Objective-C中的@package与C语言中变量和函数的private_extern类似。任何在实现类的镜像之外的代码想使用这个实例变量都会引发linkerror

Thisscope is most useful for instance variables in framework classes,where @private may be too restrictivebut @protected or @public toopermissive.

这个类型最常用于框架类的实例变量,使用@private太限制,使用@protected或者@public又太开放

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值