Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCode iPhone automatic property, synthesize and delloc

I have been developing for iPhone from last 1-2 months and all the time for taking IBOutlet I use the following method to declare any property:

In .h files:

@interface ....
{
     controlType varName;
}

@property() IBOutlet controlType varName;

In .m files:

at top -

@synthesize varName;

in delloc method - [varName release];

Although the method is good but it isn't very good when its taking a couple of minutes just for declaring property.

Is there any automatic process for declaring properties, IBOutlets, IBActions or basic data variables.

I hope there should be some automatic methods for this scenario as this is a very basic process.

Thanks all.

like image 540
necixy Avatar asked Mar 22 '11 06:03

necixy


3 Answers

Another one that not many people are aware of is that you can drag from Interface Builder to your header file to create IBActions or IBOutlets.

NOTE: Open the Assistant Editor with focus on the XIB inside IB.

If you create your IBOutlets this way Xcode automatically adds the property, synthesizes it, sets it to nil in viewDidUnload and releases it in dealloc.

You simply drag from the IB object to your header file the same way you would when creating connections between the view objects and their file owners (screenshot below). enter image description here

like image 114
Rog Avatar answered Nov 20 '22 07:11

Rog


In fact, yes!

You no longer need to declare the actual iVar.

In short, simply leave out this part: controlType varName;

For now, you do still need to explicitly "synthesize".

(As far as I can see, they could possibly automate that in the future. But for now you have to "synthesize" to create the setter and getter.)

You do still have to release the memory - there's really no way that could be automated, as memory handling is "real he-man programming."

For any new chums reading, don't forget the "self." part when using the property.

Plus note that XCode4 even has a new automatic thingy. Simply drag from the interface builder, to your .h file, and it will do everything for you -- try it!


In answer to your suplementary question: An IBOutlet DOES NOT PARTICULARLY NEED TO BE a property - you can just use a normal cheap iVar. On the other hand, if you wish, to you can use a property. Furthermore: you can even use the new trick (2011) of not bothering to declare the ivar, just use the property declaration, and shove an IBOutlet in there!

like image 4
Fattie Avatar answered Nov 20 '22 08:11

Fattie


There is an awesome tool that speeds the whole process up: accessorizer.

You write controlType varName; select it and press some buttons and accessorizer will create the property, init, dealloc, viewDidUnload and much more for you. You just have to paste the code into your project.

Try it, a demo is available.

like image 2
Matthias Bauch Avatar answered Nov 20 '22 08:11

Matthias Bauch