Re: There's obviously something I don't understand about autorelease.
Re: There's obviously something I don't understand about autorelease.
- Subject: Re: There's obviously something I don't understand about autorelease.
- From: Roland King <email@hidden>
- Date: Sun, 18 Mar 2012 09:34:07 +0800
On Mar 18, 2012, at 9:10 AM, Richard Somers wrote:
> On Mar 17, 2012, at 6:12 PM, Roland King wrote:
>
>> So often I find I start with
>>
>> @synthesize foo=_foo;
>>
>> and by the time I get to the end of the project I've written custom foo: and setFoo: methods which do something else too.
>
> I have also done that but I recently read a blog where the writer recommends "Do not override @synthesized properties with your own code: use @dynamic properties instead ...".
>
> http://wiki.akosma.com/Objective-C_Code_Standards
>
> I would be curious if anyone else has an opinion on that one way or the other.
>
> --Richard
>
I don't know what he's talking about. I don't override synthesized properties, I remove the synthesized and replace it with my own <property> and set<Property>: methods (**) and that's totally supported (and necessary if you are going to do more than just set an ivar)
Nor do I know if he understands what @dynamic means, if you are writing your own getters and setters and putting them in the .m file then you don't need @dynamic. @dynamic means "I haven't synthesized the property nor written a getter/setter for it so you want to give me a compile error. Please don't, I will make the code dynamically available at runtime, like core data does for instance".
Sorry but I take that blog entry with a pinch of salt.
Roland
(**) You don't need to remove the @synthesize either if you don't want to, you can leave it there and still write your own getter and setter, they will override. Why however would you do this. The only good reason I've found is because I've done this piece of stupidity twice now. (The code below uses ARC)
Bar.h
@interface Bar
{
Foo *_foo; // booboo on foo declaration
}
@property( readwrite, weak ) Foo *foo;
Bar.m
@synthesize foo=_foo; // syntheisze it - this gives a helpful compile error
// override it
-(Foo*)foo
{
return _foo;
}
-(void)setFoo:(Foo*)foo
{
_foo=foo;
[ self doSomethingClever ];
}
The error here is that the property foo is declared weak but the ivar _foo is strong. It's REMARKABLY easy to make that mistake when you change a property from strong to weak when for-instance you discover a retain cycle and forget to change the underlying ivar. If the @synthesize is there, the compiler catches the mismatch, very useful, if you leave it out because you don't need it, nothing catches it and you have a weak property using a strong ivar and ... you still have a retain cycle.
This leads to my last pet peeve which is that I think property syntax should have always been
@property( readwrite, weak, ivar=_foo) Foo *foo;
where the underlying ivar for the property, if there is one, is declared as part of the property declaration instead of split into the synthesize directive, but that ship I suspect has sailed a long time ago.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden