Objective-C:为何使用ivar成员变量?Why would you use an ivar?[zz]
2015-09-25 15:59
417 查看
如果ivar是私有的,其它代码无法直接访问它。对应属性,则可以任意访问(public)。
Yes, this can make a difference in some cases. Some programs have constraints where they can not use any objc messaging in certain parts of the program (think realtime). In other cases, you may want to access it directly for speed. In other cases, it's because
objc messaging acts as an optimization firewall. Finally, it can reduce your reference count operations and minimize peak memory usage (if done correctly).
比如一个C++类型,直接访问是更合理的方法。
Many of your ivars are codependent. You must ensure your data integrity in multithreaded context. Thus, you may favor direct access to multiple members in critical sections. If you stick with accessors for codependent data, your locks must typically be reentrant
and you will often end up making many more acquisitions (significantly more at times).
states -- in your initializers and in
It's also more frequent as one moves from the everything has a public readwrite accessor mindset to one which hides its implementation details/data well. Sometimes you need to correctly step around side effects a subclass' override may introduce
in order to do the right thing.
Declaring everything readwrite by default usually results in many accessor methods you never need, when you consider your program's execution for a moment. So it will add some fat to your program and load times as well.
That's not at all to say using properties or accessors is bad - each has important benefits and restrictions. Like many OO languages and approaches to design, you should also favor accessors with appropriate visibility in ObjC. There will be times you need
to deviate. For that reason, I think it's often best to restrict direct accesses to the implementation which declares the ivar (e.g. declare it
这里不是说直接访问ivar就一定比属性更好,原则上public的成员定义成属性,private的定义成ivar。
re Edit 1:
Most of us have memorized how to call a hidden accessor dynamically (as long as we know the name…). Meanwhile, most of us havenot memorized how to properly access ivars which aren't visible (beyond KVC). The class continuationhelps, but
it does introduce vulnerabilities.
This workaround's obvious:
Now try it with an ivar only, and without KVC.
http://stackoverflow.com/questions/9086736/why-would-you-use-an-ivar
Encapsulation封装
If the ivar is private, the other parts of the program can't get at it as easily. With a declared property, the clever people can access and mutate quite easily via the accessors.如果ivar是私有的,其它代码无法直接访问它。对应属性,则可以任意访问(public)。
Performance性能
直接访问ivar,原则上来说,性能更优。属性本质上是调用了getter/setter方法,也就是消息调用,开销更大。Yes, this can make a difference in some cases. Some programs have constraints where they can not use any objc messaging in certain parts of the program (think realtime). In other cases, you may want to access it directly for speed. In other cases, it's because
objc messaging acts as an optimization firewall. Finally, it can reduce your reference count operations and minimize peak memory usage (if done correctly).
Nontrivial Types非常规类型
Example: If you have a C++ type, direct access is just the better approach sometimes. The type may not be copyable, or it may not be trivial to copy.比如一个C++类型,直接访问是更合理的方法。
Multithreading多线程
在多线程环境下,需要保证数据的一致性。在苛刻的环境中,直接访问成员变量更合适。如果采用方法访问,则需要采用可重入锁等机制保证数据的一致性。Many of your ivars are codependent. You must ensure your data integrity in multithreaded context. Thus, you may favor direct access to multiple members in critical sections. If you stick with accessors for codependent data, your locks must typically be reentrant
and you will often end up making many more acquisitions (significantly more at times).
Program Correctness程序正确性
Since the subclasses can override any method, you may eventually see there is a semantic difference between writing to the interface versus managing your state appropriately. Direct access for program correctness is especially common in partially constructedstates -- in your initializers and in
dealloc, it's best to use direct access. You may also find this common in the implementations of an accessor, a convenience constructor,
copy,
mutableCopy, and archiving/serialization implementations.
It's also more frequent as one moves from the everything has a public readwrite accessor mindset to one which hides its implementation details/data well. Sometimes you need to correctly step around side effects a subclass' override may introduce
in order to do the right thing.
Binary Size二进制大小
属性定义会增加代码量(隐形代码)Declaring everything readwrite by default usually results in many accessor methods you never need, when you consider your program's execution for a moment. So it will add some fat to your program and load times as well.
Minimizes Complexity最小复杂度
In some cases, it's just completely unnecessary to add+type+maintain all that extra scaffolding for a simple variable such as a private bool that is written in one method and read in another.That's not at all to say using properties or accessors is bad - each has important benefits and restrictions. Like many OO languages and approaches to design, you should also favor accessors with appropriate visibility in ObjC. There will be times you need
to deviate. For that reason, I think it's often best to restrict direct accesses to the implementation which declares the ivar (e.g. declare it
@private).
这里不是说直接访问ivar就一定比属性更好,原则上public的成员定义成属性,private的定义成ivar。
re Edit 1:
Most of us have memorized how to call a hidden accessor dynamically (as long as we know the name…). Meanwhile, most of us havenot memorized how to properly access ivars which aren't visible (beyond KVC). The class continuationhelps, but
it does introduce vulnerabilities.
This workaround's obvious:
if ([obj respondsToSelector:(@selector(setName:)]) [(id)obj setName:@"Al Paca"];
Now try it with an ivar only, and without KVC.
相关文章推荐
- Swift - AnyObject与Any的区别
- Swift调用Objective-C编写的代码(颜色选择器KKColorListPicker调用)
- Objective-C和JS交互
- C++ 11 - STL - 函数对象(Function Object) (中)
- Objective-C---3---NSString的常用方法
- 设计模式之工厂方法模式(代码用Objective-C展示)
- Java学习笔记【Object】
- 【投稿】打造Objective-C安全的Collection类型
- Object-C 对象初始化
- Object-C 数组(一)(NSArray)
- Object-C 字符串对象
- object-c 字符串(NSString)
- 【IOS 开发学习总结-OC-18】★★objective-c面向对象之——OC的包装类与对象处理
- org.hibernate.NonUniqueObjectException的原因与解决方法
- 黑马程序员--Java基础学习笔记【Object类、String类】
- Object 转 Json (针对父类和数组)
- Objective C静态代码扫描和代码质量管理 OClint + SonarQube
- 2016校招记
- 操作JSONObject中的某一个bean
- 获取JSONObject里所有key