I don't understand this "nonatomic" business with @property

Discussion in 'Mac Programming' started by zippyfly, Jul 8, 2010.

  1. zippyfly macrumors regular

    Joined:
    Mar 22, 2008
    #1
    I've been googling around and don't really understand the attributes used for @property declarations...

    Is this only applicable if I am doing multi-thread programming?

    And in fact is multi-threading going to happen even if I do not explicitly write a multi-threaded program, if the application generates events that concurrently access the same instances? (I don't know how, I am just theorizing).

    A related question is why is there a "retain" attribute. What I mean is, shouldn't ALL setters retain then release? Why bother to set that unless you don't want to retain before releasing, and why would you do such a thing?

    Help appreciated to get me (and others googling this question) on the right track.

    Thanks!
     
  2. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #2
    This article on developer.apple.com explains it nicely:

    http://developer.apple.com/mac/libr...ceptual/ObjectiveC/Articles/ocProperties.html

    Basically retain is for Objective-C objects, assign is for most everything else (integers, ranges, points, etc). There's also copy if you want to copy the object.

    Nonatomic makes more efficient accessors but in a way that it's possible incorrect values will be set or returned if two threads try to access the same property at once. You shouldn't run into problems unless you explicitly make your program multithreaded.

    For what it's worth, nonatomic seems to be preferred in most sample code I've seen, that is, if you know your programs isn't going to run into multithreading issues it's worth making the property nonatomic.
     
  3. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #3
    the setters for delegates should also be assign, not retain.
     

Share This Page