属性“Nonatomic"是什么?意思是? [英] What does the property "Nonatomic" mean?

查看:15
本文介绍了属性“Nonatomic"是什么?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码中的非原子"是什么意思?

What does "nonatomic" mean in this code?

@property(nonatomic, retain) UITextField *theUsersName;

原子和非原子有什么区别?

What is the difference between atomic and nonatomic?

谢谢

推荐答案

查看Apple 文档.

基本上,如果您说 nonatomic,并且您使用 @synthesize 生成访问器,那么如果多个线程尝试一次更改/读取属性,则可能会发生错误.您可以获得部分写入的值或过度释放/保留的对象,这很容易导致崩溃.(不过,这可能比原子访问器快得多.)

Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)

如果您使用默认值(即 atomic;过去没有关键字,但有 现在是),那么 @synthesized 方法使用对象级锁来确保多次读/写对单个属性进行序列化.正如 Apple 文档指出的那样,这并不意味着整个对象是线程安全的,而是单个属性的读/写是.

If you use the default (which is atomic; there used to be no keyword for this, but there is now), then the @synthesized methods use an object-level lock to ensure that multiple reads/writes to a single property are serialized. As the Apple docs point out, this doesn't mean the whole object is thread-safe, but the individual property reads/writes are.

当然,如果您实现自己的访问器而不是使用 @synthesize,我认为这些声明除了表达您关于该属性是否以线程安全方式实现的意图之外什么都不做.

Of course, if you implement your own accessors rather than using @synthesize, I think these declarations do nothing except express your intent as to whether the property is implemented in a threadsafe manner.

这篇关于属性“Nonatomic"是什么?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆