Why are Objective-C delegates usually given the property assign instead of retain?
2011-11-02 16:55
357 查看
转自:http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retai
问:
I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the both are the same in a garbage collected environment. I'm
mostly concerned with a non-GC based environment (eg: iPhone).
Directly from Scott's blog:
"The assign keyword will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates."
What does it mean that you don't directly own the delegate object? I typically retain my delegates, because if I don't want them to go away into the abyss, retain will take care of that for me. I usually abstract UITableViewController away from its respective
dataSource and delegate also. I also retain that particular object. I want to make sure it never goes away so my UITableView always has its delegate around.
Can someone further explain where/why I'm wrong, so I can understand this common paradigm in Objective-C 2.0 programming of using the assign property on delegates instead of retain?
Thanks!
答:
The reason that you avoid retaining delegates is that you need to avoid a retain loop (more commonly known as retain cycle):
A creates B A sets itself as B's delegate … A is released by its owner
If B had retained A, A wouldn't be released, as B owns A, thus A's dealloc would never get called, causing both
A and B to leak.
You shouldn't worry about A going away b/c it owns B and thus gets rid of it in dealloc.
问:
I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to understand a fundamental Objective-C concept of assigning delegates the 'assign' property vs 'retain'. Note, the both are the same in a garbage collected environment. I'm
mostly concerned with a non-GC based environment (eg: iPhone).
Directly from Scott's blog:
"The assign keyword will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don't directly own, such as delegates."
What does it mean that you don't directly own the delegate object? I typically retain my delegates, because if I don't want them to go away into the abyss, retain will take care of that for me. I usually abstract UITableViewController away from its respective
dataSource and delegate also. I also retain that particular object. I want to make sure it never goes away so my UITableView always has its delegate around.
Can someone further explain where/why I'm wrong, so I can understand this common paradigm in Objective-C 2.0 programming of using the assign property on delegates instead of retain?
Thanks!
答:
The reason that you avoid retaining delegates is that you need to avoid a retain loop (more commonly known as retain cycle):
A creates B A sets itself as B's delegate … A is released by its owner
If B had retained A, A wouldn't be released, as B owns A, thus A's dealloc would never get called, causing both
A and B to leak.
You shouldn't worry about A going away b/c it owns B and thus gets rid of it in dealloc.
相关文章推荐
- Why are Objective-C delegates usually given the property assign instead of retain?
- Given an array of non-negative integers, you are initially positioned at the first index of the arra
- Given the array of digits (0 is also allowed), what is the minimal sum of two integers that are made
- These kinds of timepieces are usually far better cloned as compared to some other timepieces because of the sought after and also reputation
- Objective-c 的 @property strong,weak,assign,copy,retain等关键字介绍
- Why are some of the tables in my query missing from the plan?
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, or CouchDB?
- Objective-C 2.0属性(Property) retain,copy,assign,copy
- Diesel powered timepieces selling on the web are usually really one of the most stylish timepieces
- Given the string of parentheses only, write the function to check if they are balanced. ((())) is ba
- Why the framework uses ruby instead of perl?[转]
- Why does Quora use MySQL as the data store instead of NoSQLs such as Cassandra, MongoDB, CouchDB etc?
- the cause of the delegate why use assign
- Objective-C 属性特性(assign , retain , copy , readonly , readwrite , atomic , nonatomic)
- git The following paths are ignored by one of your .gitignore files:
- Be the Best of Whatever You Are
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Why Amazon Echo is the future of every home
- NoNodeAvailableException[None of the configured nodes are available
- Android & iOS – on the edge of Qt and Java/Objective-C