7

A friend told me that the @property default for scalar properties (BOOL, NSInteger, etc.) is nonatomic. I.e.,

@property BOOL followVenmo;

defaults to

@property (nonatomic) BOOL followVenmo;

But, I was always under the impression that the default is always atomic, scalar or not.

Which is it?

ma11hew28
  • 121,420
  • 116
  • 450
  • 651

3 Answers3

10

Be careful with this "scalar" terminology. An NSString * property is also a pointer, exactly like the example you provided with a pointer to BOOL.

From Apple docs: (The Objective-C Programming Language)

If you specify retain or copy and do not specify nonatomic, then in a reference-counted environment, a synthesized get accessor for an object property uses a lock and retains and autoreleases the returned value—the implementation will be similar to the following:

[_internal lock]; // lock using an object-level lock
id result = [[value retain] autorelease];
[_internal unlock];
return result;

You can't apply an object-level lock in something that's not an object, so (non)atomic in properties of primitive types has basically no effect.

You can conclude that atomic only applies to object properties, and this is reinforced in the docs:

If you specify nonatomic, a synthesized accessor for an object property simply returns the value directly.

To clarify whether you should specify one or the other: technically, properties without a nonatomic are considered atomic, but remember that it has no meaning for primitive types. Thus, you may want to save some typing and avoid nonatomic in these.

Community
  • 1
  • 1
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • I'm still a little unclear. So, should I not specify `nonatomic` for primitive types? – ma11hew28 Jul 26 '11 at 23:10
  • You really don't have to have any attributes for primitive types. What he is saying is that you only require nonatomic for objects, not primitive types. So @property BOOL someBool; works fine. – Lucas Derraugh Jul 26 '11 at 23:16
  • Sorry, I see it should be clearer. I edited the answer, and thanks @Lucas Derraugh – sidyll Jul 26 '11 at 23:23
  • So `_internal` is relative to `value` (if it is an object), not to the object that has the property, am I right? – Norswap Sep 17 '13 at 08:55
0

Based on my research of a couple other related questions:

I shall abide by @Rhubarb's recommendation:

As a rule of thumb, if you don't need multithreaded support - which you generally don't if you're working in UI code like UIViewControllers, then just declare it all nonatomic.

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
-1

From the Developer Documentation

nonatomic Specifies that accessors are nonatomic. By default, accessors are atomic.

Atomic properties ensures that you will get or set a whole value. For example, setting a CGRect from 2 threads will end up with one or the other, not some combination of the two.

For retained properties, it also ensures that the result can outlive the receiver. For example, you get a result from an object that is released by another thread before the call finishes, but the result is retained and autoreleased on your behalf so it is still valid.

drawnonward
  • 53,459
  • 16
  • 107
  • 112
  • 1
    Did you read the documentation? The answer is in the quote from the documentation - all accessors are atomic regardless of the property being primitive or not. – drawnonward Aug 02 '11 at 18:06