views:

123

answers:

2

Anybody have any good pointers on how to be able to, say, highlight a ivar in a class and hit a keystroke and insert the property and synthesize lines into my code in the proper places? I have been doing this for awhile now and typing that boilerplate stuff is getting on my nerves at times.

+1  A: 

http://stackoverflow.com/questions/1151393/xcode-script-for-generating-synthesizing-properties

This seems to be what you're looking for.

kubi
Note that I just added an answer there with a script that does both property and synthesize statements in one go, and also tries to get the property retain attribute correct (does not yet mark NSString vars as Copy though).
Kendall Helmstetter Gelner
A: 

I banged out a couple of quick and dirty scripts in Ruby for this. They just operate on the selection and put the results on the clipboard.

Property declerations:

#! /usr/bin/env ruby -w

r=/^\s*(.*;)/
inputLines=STDIN.read
inputLines.each do |l|
  l.strip!
  m=l.match(r)
  if m   
    if m[0].index("*") 
        puts "@property(nonatomic, retain)  " + m[0]
    elsif m[0].index(/^\s*id\s/ )
        puts "@property(nonatomic, retain)  " + m[0]
    else
        puts "@property " + m[0]
    end
  end
end

This turns... NSString *aString;

...into:

@property(nonatomic, retain)  NSString *aString;

This script turns the iVar to a @synthesize:

#! /usr/bin/env ruby -w

r=/\s*\w+\s+\**(\w+\s*);/
s=STDIN.read
s.each_line() do |l|
  l.strip!
  m=l.match(r)
  if m
    puts "@synthesize " +m.captures[0]+";"
  end
end

Scripts to do this all in one go require Applescript which gives me a rash. I stick by KISS.

TechZen
Nothing is simpler than a single operation... which doesn't require pure Applescript.
Kendall Helmstetter Gelner