Create @property, @synthesize & dealloc from Variable Declaration [video]
Drastically Improved on 2010-03-07
UPDATE: 2010-03-07 Updated to write the @synthesize and dealloc declarations to the .m file as well as place the @property declaration just below the closing bracket in the .h file. I have been testing it all day and have had no issues with it. Please let me know if you have an suggestions or find a bug. UPDATE: 2010-03-10 Updated to work on Leopard.Leopard, concerning Apple Events, is a little flaky. If you encounter any issues, please report them.
UPDATE: 2010-03-12 Updated to choose between adding the @property declarations after the closing bracket "}" in the @implementation or just before the closing @end.
Set this preference to true for the bracket and false for @end.
@position_prop_after_closing_bracket = false
One of the things we do a lot of in Objective-C is outlet and variable declaration, synthesize and dealloc. Since this is a repeatable recurring event I thought I would add a little automation to the process.
This updated script has a few preferences. One is @thread_safe. If set to true, the "nonatomic" value will be used. If set to false, it will not. The others are for memory management. Add class names to the corresponding array for @copy, @assign and @retain for default assignments for those classes. This isn't a hugh amount of preferences but it does allow some flexibility.
The most significant improvement is that it now writes the @synthesize and dealloc declarations for you in the .m file. It also positions the @property declarations just below the closing bracket in the .h file. It does not create a dealloc declaration for anything that you list in the @assign array.
Requirements / Dependencies
The script is dependent on rb-appscript. The easiest way to install this is with Ruby gems.
sudo gem install rb-appscript
Instructions
The below video demonstrates how to add this script to a new Xcode user script. Once you have done that, select the ivar declarations and run the script. If all things go well, you will see @property declarations in your .h file and @synthesize and dealloc declarations in the .m file.
You will see the document change from the .h file to the .m file and then back to the .h file again. This happens very quickly but it is normal.
How to Use This ScriptVideo does not show the latest preference added @position_prop_after_closing_bracket
Code has been moved to Github
Click me to view code on Github
If you make changes to this code and post it anywhere on the web, please make note of the fact that you have modified the code and what those modifications are. A link back to this page is appreciated. Thanks!
Now that the code is on GitHub, the best option is to fork the project, make your changes and request those changes get implemented into the original code.


Comments
- you select the ivars
- choose the menu item
- paste below the closing ivar bracket
- select & copy everything below the last @property (@synthesizes and the assignments)
- Go into .m file, paste again
- select & copy the assignments below the @synthesizes
- paste into dealloc
I currently use Accessorizer: kevincallahan.org/.../... since it has every option I could ever want.
--Cheers
**UPDATE**
I just re-read your comment and the answer is that it is already being copied to the clipboard in the line preceeding the printf line.
Nice tutorial at Masters of the Void!: masters-of-the-void.com/
/var/folders/Gn/GnoTfv2rGXe3zDA8VdNuRU+++TI /-Tmp-/042842A8-7FEB-44E7-AFBC-AB425746A546-2418-00002DEDDD4F5104:10: undefined method `strip!' for nil:NilClass (NoMethodError)
from /var/folders/Gn/GnoTfv2rGXe3zDA8VdNuRU+++TI /-Tmp-/042842A8-7FEB-44E7-AFBC-AB425746A546-2418-00002DEDDD4F5104:9:in `each'
from /var/folders/Gn/GnoTfv2rGXe3zDA8VdNuRU+++TI /-Tmp-/042842A8-7FEB-44E7-AFBC-AB425746A546-2418-00002DEDDD4F5104:9
No thank you! That works like a charm now, great work!
Gary
Just one more thing Craig, thanks for adding primitives support but it still codes the @property with retain and a pointer so;
int orderCount;
generates
@property (nonatomic, retain) int *orderCount;
when of course it should be
@property (nonatomic, assign) int orderCount;
Cheers!
Gary
You might try changing the array style from:
@assign = %w( int integer BOOL float NSUInteger NSInteger )
to:
@assign = ["int", "integer", "BOOL", "float", "NSUInteger", "NSInteger"]
% xcode-select -print-path
/Developer
whereas the beta version is installed in /Developer/Developer-3.2.2beta3/.
The application's filename or POSIX path, e.g. 'TextEdit', 'TextEdit.app', '/Applications/TextEdit.app'. Where a filename is provided, appscript uses LaunchServices to locate the application. An .app suffix is optional. e.g. Given the name 'TextEdit', appscript first searches for an application with that exact filename; if none is found, it automatically adds an .app suffix ('TextEdit.app') and tries again.
It looks like LaunchServices still has the previous version as the default.
You should be able to provide the full path to target Xcode.
Replace
Xcode = app('Xcode.app')
with
Xcode = app('/Developer/Applications/Xcode.app')
I will change this in the post code as well.
Also, I took off the captcha. I'll have to find another way around the spam. ;)
In my .h file, at the top, before the @interface line, I have several @class lines, and above those, I have a couple typedef enum {...] blocks. I tested the script with one ivar selected; the @property line that your script inserts, got inserted it in multiple places: after each typedef enum {}; block, as well as after the @interface {} block. So I ended up with 3 copies of the @property declaration inside the .h file.
Thanks
originally had:
UITextField *alphaNumField;
UITextField *numericField;
Then added a few more and just did the new ivars. It somehow got the old ones stuck in its craw.
So I delete everything and start with:
UITextField *alphaNumField;
UITextField *numericField;
int mysupercoolInt;
the .H and the @property lines are great.
the .M is still has some duplication going.
@synthesize alphaNumField;
@synthesize numericField;
@synthesize mysupercoolInt;
@synthesize alphaNumField;
@synthesize numericField;
@synthesize alphaNumField;
@synthesize numericField;
dealloc has the same dups
I tried a new file, no dups from 1st. Deleted it. I added another iVar ran the script and it remembered the ivar that I did before
So now that I've deleted the Items that the Script generates and have jsut the three ivars and nothing in the .m and no @property's I get the proper number of @property, but I get all of the old ivars that I've done. Some how it remembered that I did in a previous run of the script.
In the new file I deleted the ivar I started with and created a new one with a new name. deleted the items in the .m and ran the script. The old ivar that I deleted reappeared in the .m file.
release
astus.be/.../...
- In dealloc it releases instead of setting to nil.
- Supports IBOulets.
- Set outlets to nil in viewDidUnload if you have it on your object.
pastebin.com/7qGUaJ0j
RSS feed for comments to this post.