[Objective-c] Strong/weak Property--the metaphor of a dog collar

Source: Internet
Author: User

Turn from:

Http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign

Http://rdcworld-iphone.blogspot.in/2012/12/variable-property-attributes-or.html


BJ Homer to Strong/weak reference gave a very image of the metaphor, very in place, excerpt as follows:


Imagine Our object are a dog, and that's dog wants to run away (be deallocated).

Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog would not run away. If Five people attach their leash to one dog, (five strong to one object), pointers the then would not run dog away a LL five leashes are detached.

Weak pointers, on the other hand, are like little kids pointing at the dog and saying "look! A dog! " As long as the dog is still on the leash, the little kids can still, the dog, and they ' ll still it. As soon as all of the leashes are detached, though, the dog runs, away no matter how many, little kids and are to it.

As soon as the last strong pointer (leash) no longer points to an object, the object would be deallocated, and all weak poi Nters is zeroed out.

For the explanation of Strong/weak/retain/assign, excerpts are as follows:


Atomic//default nonatomic strong = retain Weak/Assign retain assign//default unsafe_unretained copy readonly ReadWrite Default

Below is the detailed article link where can I find above mentioned all attributes, which would definitely help you. Many to the people who give best answers here!!

Variable property attributes or modifiers in IOS

1.strong (iOS4 = retain) It says "Keep this in" heap until I don ' t point to it anymore "in other words" I ' am the Owne R, you are cannot dealloc this before aim fine with that same as retain "your use strong only if you are need to retain the object." By default all instance variables and local variables are strong pointers. We generally use strong for uiviewcontrollers (UI item ' s parents) strong is used with ARC and it basically Not has to worry about the retain count of object. ARC automatically releases it for your you are do with it. Using The keyword strong means that you own the object.

Example:

@property (Strong, nonatomic) Viewcontroller *viewcontroller;

@synthesize Viewcontroller;

2.weak-it says "Keep this as long as someone else points to it strongly" the same thing as assign, no retain or release A "weak" reference is a reference which you don't retain. We generally use weak for iboutlets (Uiviewcontroller ' s Childs). This works because the ' child object ' needs to exist as long as the ' parent object does. A weak reference is a reference this does not protect the referenced object to collection by a garbage collector. Weak is essentially assign, a unretained property. Except the "when" is deallocated the weak pointer are automatically set to nil

Example:

@property (Weak, nonatomic) Iboutlet UIButton *mybutton;

@synthesize MyButton;

Strong & Weak explanation, to BJ Homer:

Imagine Our object are a dog, and that's dog wants to run away (be deallocated).

Strong pointers are like a leash on the dog. As long as you have the leash attached to the dog, the dog would not run away. If Five people attach their leash to one dog, (five strong to one object), pointers the then would not run dog away a LL five leashes are detached.

Weak pointers, on the other hand, are like little kids pointing at the dog and saying "look! A dog! " As long as the dog is still on the leash, the little kids can still, the dog, and they ' ll still it. As soon as all of the leashes are detached, though, the dog runs, away no matter how many, little kids and are to it.

As soon as the last strong pointer (leash) no longer points to an object, the object would be deallocated, and all weak poi Nters is zeroed out.

When do we use weak?

The only time you are would want to use weak, are if you wanted to avoid retain (cycles. The parent e.g the child and t He child retains the ' parent so neither ' is ever released).

3.retain = Strong It is retained, old value is released and it are assigned retain specifies the new value should be sent R Etain on assignment and the old value sent-release retain is the same as strong. Apple says if you write retain it'll auto converted/work like strong only. Methods like "Alloc" include an implicit "retain"

Example:

@property (nonatomic, retain) nsstring *name;

@synthesize name;

4.assign Assign is the "default" and simply performs a variable assignment assign is a attribute that tells the COM Piler the synthesize the property's setter implementation I would use assign for C primitive properties and weak for we AK references to objective-c objects.

Example:

@property (nonatomic, assign) NSString *address;

@synthesize address;




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.