Overwriting Setters for Retain Properties
Is there any way to avoid this kind of code when overwriting the default setter for a retain
property?
-(void)setMasterViewCon开发者_开发百科troller:(UIViewController *)newMaster {
[newMaster retain];
[masterViewController release];
masterViewController = newMaster;
// do custom stuff on set
}
Is there any way to access the default setter, something like:
-(void)setMasterViewController:(UIViewController *)newMaster {
[defaultSetMasterViewController:newMaster];
// do custom stuff
}
This would keep the code DRYer. The way I'm doing it currently, the fact that it's a retain
property is mentioned twice.
CoreData generates primitive setters, but in general there's no such affordance. You may be able to replace custom setters with key-value observing in some cases, but the solution to your specific question is probably "use ARC" if you can limit support to 10.6+/4.3+. It will handle the retain/release stuff on your behalf.
Not really, because the setter has to perform the actual setting. You could try doing this using key-value observing if you want to keep the original setter.
However... Yes, if you're using ARC! If you have a @property (strong)
, then when you simply say masterViewController = newMaster
ARC will use objc_storeStrong
, which:
Performs the complete sequence for assigning to a
__strong
object of non-block type. Equivalent to the following code:id objc_storeStrong(id *object, id value) { value = [value retain]; id oldValue = *object; *object = value; [oldValue release]; return value; }
精彩评论