OC 中 @private @protected @public @package 的含义
来源:互联网 发布:淘宝手机端主图片尺寸 编辑:程序博客网 时间:2024/06/10 19:15
Brief
Detail
关于 @package 在 StackOverFlow 有一个比较清晰的解答,摘录如下:
Instance variables that are declared under @package will only be accessible by code from the same framework, library or executable. This is most similar to internal in C# and Friend in VB.NET, but other programming languages have similar protections schemes as well (Java has package-private).
For instance, imagine a framework, RecipeKit.framework, for managing recipes:
@interface Recipe : NSObject { @package; NSString *name; @private; NSArray *ingredients;}@end@interface RecipeController : NSObject@end
The recipe controller will be able to access the names of recipes directly. You would be able to write something like this in the RecipeController since it’s part of the same framework:
Recipe *recipe = [[[Recipe alloc] init] autorelease];[recipe->name autorelease];recipe->name = [@"The best ever recipe!" retain];
If you were to write the above code in an application linking to RecipeKit, though, it would cause a compiler error since you don’t have access to that variable. Lastly, when compiled for 32-bit, variables declared under @package behave as if they were declared under @public instead, so watch out for the differences here.
This new feature got very little attention because it’s just one more way of breaking you’re class’s encapsulation. As has always been true, you’re probably better off working with @private variables only and having accessor methods for them. In fact, for a while Apple was trying to push this by including @private in their templates. With properties in Objective-C 2.0, sticking with @private is easy enough, and depending on what platform you’re targeting, you can leave out the instances variables entirely.
Images
One thing that a few of the previous answers left a little unclear was addressing the aspect of images from the original question. In fact, the word image as used in the description of @package variables has nothing to do with graphical images. Instead it is referring to images that the dynamic linker is able to load. Generally executables, frameworks, and dynamic libraries are all considered images by the linker (though they are handled slightly differently). You’ll see the word image pop up every once and a while. For instance, a common runtime error is: “dyld image not found”. You’ll also find the use of the word image scattered throughout documentation for dyld. Reading through the man page for dyld could help clear up the ambiguity of this word a bit. UIImage may declare variables as @package, but it has nothing to do with image as it pertains to the original question.
Works Cited
Objective-C Runtime Release Notes for OS X v10.5
What does the @package directive do in Objective-C?
- OC 中 @private @protected @public @package 的含义
- OC中类变量的@protected,@private,@public,@package区别
- OC中类变量的@protected,@private,@public,@package区别
- OC中public、protected、private 的区别?
- Java中public、package、protected、private的访问权
- Java中public、protected、package、private的访问权
- Delphi:class 中protected、private、public 和 published 的含义
- @protected ,@private,@public,@package的使用
- 9、OC 访问控制符(@private @package @protected @public)
- java中的public private protected的含义
- 类变量的@protected ,@private,@public,@package,声明各有什么含义?
- Objective-C中@private、@protected、@public、@package 解析
- java中private, public,protected的区别
- Java中private、public、protected的区别
- C++中private,public,protected的访问
- java中private, public,protected的区别
- java中private, public,protected的区别
- C++中public、protected、private的权限
- 通过实时日志分析_进行访问日志的快速统计
- 使用ajax提交form表单,包括ajax文件上传
- coderforces Stones on the Table
- 30. secure world对smc请求的处理------OP-TEE对fast smc请求的处理
- linux shell 带颜色输出
- OC 中 @private @protected @public @package 的含义
- Android录屏功能的实现
- /dev/null简单入门
- Kinect开发 骨骼示意图
- 40个Java多线程问题总结
- 用sql生成EF上下文
- Thinking in java-3 Java 堆和栈
- 线性表链式存储结构
- 在分布式系统中进行日志查询与管理的新手段介绍