Login

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 Script
Video 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  

 
0 # Uli Kusterer 2010-02-14 03:57
Wouldn't it be a better idea to set the output to "Clipboard"? Saves you one copy/paste. I.e.:
- 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
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-02-14 14:38
If that fits your workflow, then I say go for it!

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/
Reply | Reply with quote | Quote
 
 
0 # gary 2010-02-25 04:39
Well it doesnt work for me (running xcode 3.1.4) when I do exactly as the video describes (and I have checked and double checked) I get this output

/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
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-02-25 22:32
@gary - I have altered the code to account for using it with primitives. Thanks for the bug report.
Reply | Reply with quote | Quote
 
 
0 # gary 2010-02-26 02:13
@Craig,

No thank you! That works like a charm now, great work!

Gary
Reply | Reply with quote | Quote
 
 
0 # Alec 2010-03-09 18:23
Nice work Craig! One little problem on my end though. I try to install app script but in Terminal it just says error downloading appscript. I followed a tutorial on another site but it doesn't work either. Is there some other way on installing appscript?
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-09 18:52
Look at this page. allancraig.net/.../
Reply | Reply with quote | Quote
 
 
0 # Alec 2010-03-10 00:10
It's not working now. I got it to install appscript but the actual script now wont run. Select everything then hit C4.
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-10 07:12
Did you mean command + 4? Also, you only select the ivars, not the entire document. Let me know if that works.
Reply | Reply with quote | Quote
 
 
0 # gary 2010-03-10 04:24
@craig

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
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-10 07:21
@gary - Is int is still in the assign array?
You might try changing the array style from:
@assign = %w( int integer BOOL float NSUInteger NSInteger )
to:
@assign = ["int", "integer", "BOOL", "float", "NSUInteger", "NSInteger"]
Reply | Reply with quote | Quote
 
 
0 # Randy 2010-03-10 13:36
So I installed it, and when I try to run it, it insists on launching the BETA version of Xcode I have installed (in a non-standard directory), instead of the version that's *actually* running, and trying to do it's work there, which of course fails since there are not even any documents open. The beta version isn't even the system default one, as specified by the 'xcode-select' command:

% xcode-select -print-path
/Developer

whereas the beta version is installed in /Developer/Developer-3.2.2beta3/.
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-10 14:10
This is from the rb-appscript manual.

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. ;)
Reply | Reply with quote | Quote
 
 
0 # Randy 2010-03-10 14:56
OK, yep, that worked. However...

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.
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-10 15:19
I think that is fixed now. Thanks for all this input!
Reply | Reply with quote | Quote
 
 
0 # scooter 2010-03-13 22:11
I started using your script from Programtic Bookshelf. Was having an issue after I upgraded to SDK 3.2 Went to your site and found a bonus, upgraded Script! Sweet, thank you.

Thanks
Reply | Reply with quote | Quote
 
 
0 # scooter 2010-03-13 22:16
Reading though the features I think I found a bug.

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
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-15 12:03
The script does not check existing declarations for duplicates. This can be done but would require a much larger script. If you highlight an ivar and run the script, then highlight the ivar and run again, you will have duplicates.
Reply | Reply with quote | Quote
 
 
0 # scooter 2010-03-15 16:10
That is not wha tis happening to me. I only highlight the new ivar.

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.
Reply | Reply with quote | Quote
 
 
0 # scooter 2010-03-13 22:41
Does not seem to matter if its copy, assign, retain, It always makes it nil in the dealloc instead of the copy's being release.
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-03-15 12:06
The behavior of the script is to not add a dealloc declaration for ivars that are in the assign array. Currently, it only sets the ivar to nil in the dealloc.
Reply | Reply with quote | Quote
 
 
0 # scooter 2010-03-15 16:22
I changed the line:
release
Reply | Reply with quote | Quote
 
 
0 # Karthik 2010-03-25 15:34
Thanks a million dude.. it works like a charm for me..
Reply | Reply with quote | Quote
 
 
0 # Joris Mans 2010-07-21 05:45
I added the generation of the initializer too in the constructor of the object, since it is not really correct to have uninitialized vars.

astus.be/.../...
Reply | Reply with quote | Quote
 
 
0 # Alfred R. Baudisch 2010-08-22 14:10
I made some changes:
- 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
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

 

Product Categories