Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Objective-C with ARC, is it true that we usually only need to specify nonatomic as property attributes?

It is strange that in Big Nerd Ranch iOS 5 book (p.73) and Programming iOS 5 book (O'Reilly, p.314) (updadte: even Kochan's Objective-C book Fourth edition), in the context of ARC, they say the default for properties attribute is assign... But Apple's documentation says the default is strong.

I also tried a simple program where if I don't specify strong, the program works ok, and if I specify strong, it works the same, and when assign is used instead, the compiler shows a warning, so it seems the default is indeed strong.

So if most of the time, we want

@property (nonatomic, readwrite, strong) NSMutableArray *foo;

then we can just write

@property (nonatomic) NSMutableArray *foo;

as the other two (readwrite and strong) are the default?

like image 234
nonopolarity Avatar asked Jun 14 '12 20:06

nonopolarity


People also ask

What is Nonatomic Objective-C?

In Objective-C the implementation of an atomic property allows properties to be safely read and written from different threads. For nonatomic properties, the underlying pointer of a read value could be released when a new value is being written at the same time.

What is strong and Nonatomic in Objective-C?

nonatomic property means @synthesize d methods are not going to be generated threadsafe -- but this is much faster than the atomic property since extra checks are eliminated. strong is used with ARC and it basically helps you , by not having to worry about the retain count of an object.

What is assign property in Objective-C?

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.

What is the difference between atomic and nonatomic synthesized properties?

Atomic means only one thread accesses the variable (static type). Atomic is thread-safe, but it is slow. Nonatomic means multiple threads access the variable (dynamic type). Nonatomic is thread-unsafe, but it is fast.


1 Answers

readwrite and strong, are indeed the default under ARC*. Under manual reference counting, assign was (is) the default. I prefer to explicitly specify these, because it makes it clearer what the @property's parameters are instead of relying on the person reading the code knowing what the defaults are.

*strong is the default assuming you've either let the compiler synthesize an instance variable for you, or have declared an instance variable without an explicit ownership qualifier (in which case the ivar is __strong by default anyway). Otherwise, the default property ownership type matches the ownership qualifier in the ivar's declaration. So, if you explicitly declare an ivar with __weak, then declare an @property for it without an ownership qualifier, the synthesized property will be weak. This is all documented in the Clang ARC documentation.

like image 108
Andrew Madsen Avatar answered Oct 13 '22 19:10

Andrew Madsen