search

Home  >  Q&A  >  body text

在objective-c中atomic和nonatomic有什么区别?retain有什么用?

比如下面的代码

@property(nonatomic, retain) UITextField *userName;
@property(atomic, retain) UITextField *userName;

他们有啥区别,retain在这里起啥作用

高洛峰高洛峰2769 days ago462

reply all(4)I'll reply

  • ringa_lee

    ringa_lee2017-04-21 10:59:07

    retain is to add 1 to the reference count of the attribute
    The retain here means that this setter will add 1 to the reference count of the parameter. For example:

    self.userName = uName;

    At this time, the reference count of uName will be increased by 1.
    However, SDK 5.0 and later supports ARC, which means automatic application counting. Therefore, there is no need to retain and copy when defining attributes. Instead, use strong to let ARC manage it.
    An article providing apple core for reference: http://pingguohe.net/2011/08/05/llvm3...

    reply
    0
  • 黄舟

    黄舟2017-04-21 10:59:07

    Atomic is thread safe and has lower performance than nonatomic. noatomic does not guarantee thread safety.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-21 10:59:07

    atomic does not mean thread safety, it just means that set and get operations on the same object are executed sequentially.

    reply
    0
  • 阿神

    阿神2017-04-21 10:59:07

    The difference between iOS atomic and nonatomic

    reply
    0
  • Cancelreply