ios 属性“Nonatomic”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/821692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:52:48  来源:igfitidea点击:

What does the property "Nonatomic" mean?

iosobjective-ciphonecocoaproperties

提问by Jesse Rusak

What does "nonatomic" mean in this code?

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

@property(nonatomic, retain) UITextField *theUsersName;

What is the difference between atomic and nonatomic?

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

Thanks

谢谢

回答by Jesse Rusak

Take a look at the Apple Docs.

看看苹果文档

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.)

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

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.

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

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.

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

回答by swiftBoy

After reading so many Articles and StackOverflow posts, and having made demo apps to check Variable property attributes, I decided to put all the attributes information together

在阅读了这么多文章和 StackOverflow 帖子,并制作了用于检查变量属性属性的演示应用程序后,我决定将所有属性信息放在一起

  1. atomic //default
  2. nonatomic
  3. strong=retain //default
  4. weak= unsafe_unretained
  5. retain
  6. assign //default
  7. unsafe_unretained
  8. copy
  9. readonly
  10. readwrite //default
  1. 原子 //默认
  2. 非原子的
  3. 强=保留//默认
  4. 弱 = unsafe_unretained
  5. 保持
  6. 赋值 //默认
  7. unsafe_unretained
  8. 复制
  9. 只读
  10. 读写 //默认

so below is the detailed article link where you can find above mentioned all attributes, that will definitely help you. Many thanks to all the people who give best answers here!!

所以下面是详细的文章链接,您可以在其中找到上述所有属性,这肯定会对您有所帮助。非常感谢所有在这里给出最佳答案的人!!

Variable property attributes or Modifiers in iOS

iOS 中的变量属性属性或修饰符

  1. atomic
    • Atomic means only one thread access the variable (static type).
    • Atomic is thread safe.
    • But it is slow in performance.
    • Atomic is default behavior.
    • Atomic accessors in a non garbage-collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value.
    • it is not actually a keyword.
  1. 原子
    • 原子意味着只有一个线程访问变量(静态类型)。
    • 原子是线程安全的。
    • 但它的性能很慢。
    • 原子是默认行为。
    • 非垃圾收集环境中的原子访问器(即使用保留/释放/自动释放时)将使用锁来确保另一个线程不会干扰值的正确设置/获取。
    • 它实际上不是关键字。

Example :

例子 :

@property (retain) NSString *name;

@synthesize name;
  1. nonatomic
    • Nonatomic means multiple thread access the variable (dynamic type).
    • Nonatomic is thread unsafe.
    • But it is fast in performance.
    • Nonatomic is NOT default behavior; we need to add nonatomic keyword in property attribute.
    • it may result in unexpected behavior, when two different process (threads) access the same variable at the same time.
  1. 非原子的
    • 非原子意味着多线程访问变量(动态类型)。
    • 非原子是线程不安全的。
    • 但它的性能很快。
    • 非原子不是默认行为;我们需要在属性属性中添加非原子关键字。
    • 当两个不同的进程(线程)同时访问同一个变量时,它可能会导致意外行为。

Example:

例子:

@property (nonatomic, retain) NSString *name;

@synthesize name;

回答by Marc Charbonneau

In addition to what's already been said about threadsafeness, non-atomic properties are faster than atomic accessors. It's not something you usually need to worry about, but keep it in mind. Core Data generated properties are nonatomic partially for this reason.

除了已经说过的线程安全之外,非原子属性比原子访问器更快。这不是您通常需要担心的事情,但请记住。由于这个原因,核心数据生成的属性是非原子的。

回答by joshdick

In a multi-threaded program, an atomic operation cannot be interrupted partially through, whereas nonatomic operations can.

在多线程程序中,原子操作不能被部分中断,而非原子操作可以。

Therefore, you should use mutexes (or something like that) if you have a critical operation that is nonatomic that you don't want interrupted.

因此,如果您有一个不想被中断的非原子的关键操作,您应该使用互斥锁(或类似的东西)。

回答by Paul Tomblin

If you specify "atomic", the generated access functions have some extra code to guard against simultaneous updates.

如果指定“atomic”,生成的访问函数有一些额外的代码来防止同时更新。

回答by Jake

Usually atomic means that writes/reads to the property happen as a single operation. Atomic_operation

通常原子意味着对属性的写入/读取作为单个操作发生。 原子操作

回答by Easwaramoorthy K

You can able to get a handle of this stuffs by reading the below article.

您可以通过阅读以下文章来掌握这些东西。

Threading Explained with the nonatomic's purpose

用非原子的目的解释线程

nonatomic - Not Thread Safe

非原子 - 不是线程安全的

atomic - Thread Safe - This is the default property attribute.

atomic - 线程安全 - 这是默认的属性属性。

回答by AbcTest

The "atomic”means that access to the property is thread-safe. while the "nonatomic"is the opposite of it. When you declare a property in Objective-C the property are atomic by default so that synthesized accessors provide robust access to property in a multithreaded environment—that is, the value returned from the getter or set via the setter is always fully retrieved or set regardless of what other threads are executing concurrently. But if you declare property as nonatomic like below

“原子”的手段,获得的属性是线程安全的。而‘非原子’是它的对面。当你声明的Objective-C的一个属性的属性都默认原子,使合成的访问器提供强大的访问属性在多线程环境中——也就是说,无论其他线程正在并发执行什么,总是完全检索或设置通过 setter 从 getter 或 set 返回的值。但是如果你像下面这样将属性声明为非原子的

@property (nonatomic, retain)  NSString *myString;

then it means a synthesized accessor for an object property simply returns the value directly. The effect of the nonatomic attribute depends on the environment. By default, synthesized accessors are atomic. So nonatomic is considerably faster than atomic.

那么它意味着一个对象属性的合成访问器直接返回值。非原子属性的效果取决于环境。默认情况下,合成访问器是原子的。所以非原子比原子快得多。

回答by AbcTest

One is for multi threads. One isnt

一种用于多线程。一个不是