views:

60

answers:

3

Just a quick question if I may, I have always (since learning obj-c) written int properties as follows...

@property(nonatomic, assign) int myValue;

I am now thinking that this might be overkill and I might as well write...

@property int myValue;

Just curious, I know that "assign" is the default behaviour and that "nonatomic" can be fractionally faster ...

all the best

Gary

A: 

Nonatomic generates less code and is slightly faster. I do agree it's very verbose though.

If your app may use several threads, you'll be glad to have put those explicit qualifiers when you encounter weird bugs...

jv42
+4  A: 

Well, the question of declaring atomicity is a separate one from whether you explicitly declare attributes that are the default. The former will depend on the nature of your code -- is there likely to be contentious multithread access to the property?

Regarding the latter, it's mostly a matter of style, but in general I think you should err on the side of explicit clarity. Exactly how many seconds of your life does it cost to type those extra few keystrokes?

walkytalky
+3  A: 
@property(nonatomic, assign) int myValue;

is not the same as

@property int myValue;

The default for a property is atomic (there's no explicit keyword atomic). So the second example is an atomic property. For a property of primitive type I don't know if any locking occurs, but probably a memory write barrier will be introduced (i.e. the setter will force the value to be written back to main memory, instead of just to the cache). Thus atomic properties are considerably slower than nonatomic properties. However, as a fraction of the total execution time of the program, they could still represent a very small percentage. You won't know until you profile your code.

JeremyP