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

文档上记录是这样的

The Scope of Instance Variables

To enforce the ability of an object to hide its data, the compiler limits the scope of instance variables—that is, limits their visibility within the program. 

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

But to provide flexibility, it also lets you explicitly set the scope at four levels. Each level is marked by a compiler directive:

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

Directive

Meaning

@private

The instance variable is accessible only within the class that declares it.

@protected

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

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

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

 

@public

The instance variable is accessible everywhere.

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

 

@package

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

The @package scope for Objective-C instance variables is analogous toprivate_extern for C variables and functions. Any code outside the class implementation’s image that tries to use the instance variable gets a link error.

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

This scope is most useful for instance variables in framework classes, where@private may be too restrictive but @protected or @public too permissive.

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

 

Objective-C 对存取权限的设定。也是变量的作用域。

protected —Methods defined in the class and any subclasses can directly access the instance variables that follow.This is the default case. 该类和所有的子类中的方法可以直接访问这样的变量,这是默认的。

private —Methods defined in the class can directly access the instance variables that follow, but subclasses cannot. 该类中的方法可以访问这样的变量,子类不可以。

public —Methods defined in the class and any other classes or modules can di- rectly access the instance variables that follow. 除了自己和子类中的方法外,也可以被其他类或者其他模块中的方法所访问。开放性最大。

package —For 64-bit images, the instance variable can be accessed anywhere within the image that implements the class. 对于64位图像,这样的成员变量可以在实现这个类的图像中随意访问。

 

经过研究,@package变量,对于framework内部,相当于@protected, 对于framework外部,相当于@privat。

这个特性,很适合用于开发第三方的静态类库,因为多数人并不希望让别人知道自己属性的值。

 

写了一点代码测试一下这个作用域:

@interface TestClass : NSObject {

@private
NSString *mNamePrivate;
@package
NSString *mTextPackage;

}

-(void)print;

@end

 

@implementation TestClass

-(id)init
{
self=[super init];
if (self) {
mNamePrivate = [[NSString alloc] initWithUTF8String:"mNamePrivate"];
mTextPackage = [[NSString alloc] initWithUTF8String:"mTextPrivate"];
}
return self;
}

-(void)print
{
NSLog(@"private:%@, package:%@", mNamePrivate, mTextPackage);
}

-(void)dealloc
{
[mNamePrivate release];
[mTextPackage release];
[super dealloc];
}

 

在其它的类里面访问package作用域的成员变量是可以的,需要使用->这种访问方式。

TestClass *t = [[TestClass alloc] init];
[t print];
NSLog(@"%@", t->mTextPackage);//OK
NSLog(@"%@", t->mNamePrivate);//编译错误,提示“instance variable 'mNamePrivate' is private”
NSLog(@"%@", [t mNamePrivate]);//编译提示“mNamePrivate找不到,”程序可以运行,但是会出错
[t release];

 

posted @ 2015-06-28 11:04  stevenwuzheng  阅读(841)  评论(0编辑  收藏  举报