I know @synthesize
has already reduced lot of my work of writing getters and setters. But one common procedure I often have to use are these 4 steps for example
SomeView *abc;
@property(nonatomic,retain)SomeView *abc;
开发者_如何学Go@synthesize abc;
[abc release];
Has someone come up with any idea where I write SomeView *abc
; in .h
and steps 2,3,4 are generated automatically?
Skip step one, it is not necessary, the @synthesize will create the ivar.
If it's an IBOutlet
, Xcode will write all that code for you when you create the variable using IB. You just drag from the object you want to reference in the interface editor to the location in the header file where you want the property declaration, fill out a form, and hit OK.
As others have mentioned, you can skip declaring the backing ivar in favor of having the @synthesize
generate it for you.
You can skip the @synthesize
by using the appropriate compiler flags. Search the web for something like "default @synthesize".
One way to skip -dealloc
is to dangle the objects off your main object using the Obj-C associated object API. Retained associated objects will be released when the object they are associated with is released.
And then there's Automatic Reference Counting (ARC), which eliminates -dealloc
much more cleanly and definitively.
It's not a lot of help, but one thing I do is move dealloc to the top of the implementation, ahead of other methods. The @synthesize statements are right above that, so you can put in the @synthesize and the release often without having to scroll. It's not really a big time-saver when coding, but it does help you keep the two sections in sync, and that's a time-saver when debugging.
It's not a bad thing to wish for. And it's done for you already in some cases:
In Xcode 4's Interface Builder when you drag an element onto it's file's owner's .h file it does this 1-4 for you, and even sets it to nil in viewDidUnload
for you.
- Not needed with the modern runtime.
- Still need to do this.
- Doing away with this was talked about in the WWDC10 (113 and 144) sessions, but the release notes say that this hasn't happened yet.
- Use ARC, and you don't need this.
That's 2 of the 4 that you don't need to do, and one that they are working on removing. So it's not all there yet - but it is getting easier.
精彩评论