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

2

8

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?

Fructiferous answered 14/6, 2012 at 20:55 Comment(3)
Assign is only used with none ARC which is why you get the compiler warning.Linhliniment
but the warning is not about using assign in ARC. It is about the object will be released sooner than usually expectedFructiferous
assign is the same as unsafe_unretained under ARC. In other words, the behavior is the same as with assign under MRC.Salmi
S
11

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.

Salmi answered 14/6, 2012 at 21:10 Comment(5)
that's true... if for other people to read the code... do you specify just nonatomic, strong then?Fructiferous
by the way... even Kochan's Objective-C book says the default is not strong... so maybe something changed recently...Fructiferous
As stated in my answer, strong is the default under ARC. assign is the default under manual memory management (i.e. non-ARC), so yes, the default effectively changed for new code when ARC was introduced. I always explicitly specify the ownership when I declare properties.Salmi
Ahh, OK. In that case they're wrong. The documentation I linked to in my answer confirms that strong is the default under ARC.Salmi
You're right. It was in LLVM 3.1 to be precise, which explains the books' differing from the current situation.Salmi
I
0

By default, an object property is strong, atomic, readwrite. See https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

Indohittite answered 14/6, 2015 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.