Automatic Reference Counting (ARC) introduces some new type qualifiers. I’ve seen __strong and __weak, but what do they do?
Automatic Reference Counting (ARC) introduces some new type qualifiers. I’ve seen __strong and __weak
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
__strongmeans that on assignment, the rvalue of the expression will be retained and stored into the lvalue using primitive semantics. (To deallocate such an object, all you must do is assign itnil, the previously referenced object will be released,nilwill be retained, which effectively does nothing and it’s peaches and cream.)__unsafe_unretainedand__weakare similar in the sense that the address of the rvalue will be assigned to the lvalue, but if you use the__weakqualifier, this operation is guaranteed to be atomic and subject to some different semantics. One of these are that if the object that is being assigned is currently undergoing deallocation, then the assignment will evaluate toniland that will then be atomically stored back in to the lvalue of the expression. Hence the wording__unsafe_unretained, because that operation is indeed unsafe and unretained.__autoreleasingis like__strongexcept it has one caveat: The retained object is pushed onto the current autorelease pool, so you can for example obtain temporary ownership of an object to remove it from a collection and then return it back to the caller. There are other uses for this, but they mostly have to do with obtaining temporary ownership of an object.These behaviors also present themselves in the corresponding property modifiers (
strong,unsafe_unretainedandweak).See the Clang Automatic Reference Counting Technical Specification
EDIT: For those not targeting iOS 5 and therefore unable to reap the benefits of
__weak, Mike Ash wrote a superb article (and implementation) on zeroing weak references which you can use instead.