16

I have seen readwrite on int, BOOL etc same as nonatomic, assign.

I am some what confused on this. I do know that on non native objects, we typically do nonatomic, retain.

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
jini
  • 10,563
  • 34
  • 95
  • 166
  • See also: [Atomic vs nonatomic properties](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties) – PengOne Aug 16 '11 at 21:22
  • See also: objective c 101 (retain vs assign) @ http://stackoverflow.com/questions/1380338/objective-c-101-retain-vs-assign-nsstring – Ben.Vineyard Aug 16 '11 at 21:27

3 Answers3

31

Here's the short answer:

atomic vs nonatomic primarily ensures that complete values are returned from synthesized getters and that complete values are written by synthesized setters (atomic is default.)

readwrite vs readonly determines whether a synthesized property has a synthesized accessor or not (readwrite has a setter and is the default, readonly does not).

assign vs retain vs copy determines how the synthesized accessors interact with the Objective-C memory management scheme:

  • assign is the default and simply performs a variable assignment
  • retain specifies the new value should be sent -retain on assignment and the old value sent -release
  • copy specifies the new value should be sent -copy on assignment and the old value sent -release.
Ryan B
  • 3,338
  • 19
  • 35
PengOne
  • 47,332
  • 17
  • 126
  • 147
18

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

  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

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

Variable property attributes or Modifiers in iOS

  1. retain = strong
    • it is retained, old value is released and it is assigned
    • retain specifies the new value should be sent -retain on assignment and the old value sent -release
    • retain is the same as strong.
    • apple says if you write retain it will auto converted/work like strong only.
    • methods like "alloc" include an implicit "retain"

Example:

@property (nonatomic, retain) NSString *name;

@synthesize name;
  1. assign
    • assign is the default and simply performs a variable assignment
    • assign is a property attribute that tells the compiler how to synthesize the property's setter implementation
    • I would use assign for C primitive properties and weak for weak references to Objective-C objects.

Example:

@property (nonatomic, assign) NSString *address;

@synthesize address;
  1. readonly

    • declaring your property as readonly you tell compiler to not generate setter method automatically.
    • Indicates that the property is read-only.
    • If you specify readonly, only a getter method is required in the @implementation block. If you use the @synthesize directive in the @implementation block, only the getter method is synthesized. Moreover, if you attempt to assign a value using the dot syntax, you get a compiler error.

Example:

@property (nonatomic, readonly) NSString *name;

@synthesize name;
  1. readwrite
    • setter and getter generated.
    • Indicates that the property should be treated as read/write.
    • This attribute is the default.
    • Both a getter and setter method are required in the @implementation block. If you use the @synthesize directive in the implementation block, the getter and setter methods are synthesized.

Example:

@property (nonatomic, readwrite) NSString *name;

@synthesize name;
swiftBoy
  • 33,793
  • 26
  • 129
  • 124
  • @jrturton Please read [this](http://stackoverflow.com/questions/9784762/strong-weak-retain-unsafe-unretained-assign) and [this](http://stackoverflow.com/questions/11121839/differences-between-weak-and-unsafe-unretained) and correct me If I am wrong!! – swiftBoy Mar 21 '13 at 07:47
  • 2
    The second link says it all. unsafe_unretained does not nil out the reference when it is deallocated, so you are left with a dangling pointer, whereas a weak property is set to nil when the object is deallocated. Pre-ios 4.3, weak was not supported. – jrturton Mar 21 '13 at 09:40
16

readwrite means that both a getter and a setter exist; the opposite is readonly. Normally the only time you'd explicitly declare a property readwrite is in a class extension for a class where the public interface declares the property readonly — so that it's publicly read-only, but internally you can both get and set.

Chuck
  • 222,660
  • 29
  • 289
  • 383
  • 2
    very good answer. it is important to know when `readwrite` should even been used – NoodleOfDeath Jul 16 '14 at 12:10
  • @Chuck , the setter of readwrite will be equivalent to assign's or retain's setter? – Vishal Singh May 24 '17 at 11:46
  • @VishalSingh: `readwrite` doesn't specify memory management. You'd want to *also* list a memory management attribute for the property. – Chuck May 24 '17 at 23:28
  • Got it, thanks. In my test, I declared a property as `readonly` in `.h`, and declared same property as `weak` and `readwrite` in `.m`, but compiler complained that it can not be weak as it is declared as strong in `.h`. Guess just `readonly` attribute will be considered as backed by a `__strong` iVar. – Vishal Singh May 25 '17 at 04:55
  • 1
    @VishalSingh: Under ARC, object properties are considered `strong` by default — it doesn't have to do with `readonly` or anything else, it's just `strong` is the default for object types. So if you want a property defined this way to be `weak`, the header declaration will need to be `@property (weak, readonly)`. I know it looks awkward to declare memory management for a variable that's supposedly read only, but that's how the language needs it to be. – Chuck May 25 '17 at 18:55