Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster way to do @property , @synthesize and release a class variable?

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

  1. SomeView *abc;
  2. @property(nonatomic,retain)SomeView *abc;
  3. @synthesize abc;
  4. [abc release];

Has someone come up with any idea where I write SomeView *abc; in .h and steps 2,3,4 are generated automatically?

like image 948
Pradhan Avatar asked Sep 06 '11 01:09

Pradhan


4 Answers

Skip step one, it is not necessary, the @synthesize will create the ivar.

like image 79
zaph Avatar answered Nov 16 '22 02:11

zaph


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.

like image 38
Jeremy W. Sherman Avatar answered Nov 16 '22 01:11

Jeremy W. Sherman


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.

like image 1
Hot Licks Avatar answered Nov 16 '22 03:11

Hot Licks


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.

  1. Not needed with the modern runtime.
  2. Still need to do this.
  3. 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.
  4. 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.

like image 1
Abizern Avatar answered Nov 16 '22 03:11

Abizern