Login

Create @property, @synthesize & dealloc from Variable Declaration [video]

Now a Gem!

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.

Click me to view code on Github

Comments  

 
0 # 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 # 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 # 2010-02-26 02:13
@Craig,

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

Gary
Reply | Reply with quote | Quote
 
 
0 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 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 # 2010-03-15 16:22
I changed the line:
release
Reply | Reply with quote | Quote
 
 
0 # 2010-03-25 15:34
Thanks a million dude.. it works like a charm for me..
Reply | Reply with quote | Quote
 
 
0 # 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 # 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
 
 
0 # 2010-11-15 07:37
the script is nice thanks. one big caveat however, adding [property dealloc] to the dealloc method is completely wrong - from apple's docs about dealloc:


You never send a dealloc message directly. Instead, an object’s dealloc method is invoked indirectly through the release NSObject protocol method (if the release message results in the receiver's retain count becoming 0). See Memory Management Programming Guide for more details on the use of these methods.
Reply | Reply with quote | Quote
 
 
0 # Craig Williams 2010-11-15 10:39
Thanks for pointing that out. Fixed on GitHub.
Reply | Reply with quote | Quote
 
 
0 # 2010-11-24 06:49
I don't think to create 2 separate files to set @outlet to true or false is a good idea. Why not you just do a simple task like checking for the prefix?

If the prefix starts with UI (for most projects), then it would be more likely to be an IBOutlet rather than not
Reply | Reply with quote | Quote
 

Add comment


Security code
Refresh

 

Product Categories