RSS
 

Archive for December, 2009

Generating Objective-C Code With Ruby: Part 2 — Dealloc

16 Dec

Something that I often forget until days into working with a new controller is the dealloc method. There are a lot of steps involved in setting up a IBOutlet or property. I typical notice all at once and need to add releases for somewhere between 3 and 12 objects. This is a great time to use Ruby to save some typing.

Now there is some debate out there in the Cocoa world about whether or not you need to release objects pointed to from IBOutlets when you use a NIB to initialize. On the iPhone, if you are using an @property(retain), the answer is: “YES, YOU NEED TO RELEASE!!!”. Setting the pointer to nil also makes sense in many situations, but is not needed in the dealloc. The pointer is going to be gone very shortly anyway.

Now, this is a problem that is trivial to solve with Ruby. I want to give a script the declarations from my header file and have it give me back the code I need in my dealloc method. Of course I only want to release objects, so i skip any lines that don’t contain a “*”. If you are using a lot of untyped pointer (id), you may want to add support for that. Thanks to Joe Pecoraro for helping me make this code more rubytastic.

#!/usr/bin/ruby

puts
File.open(ARGV[0]).each do |line|
  line = line.split
  if line[0] == "IBOutlet"
      puts "[#{line[2].tr('*;','')} release];"
  elsif line[1] and line[1].include? "*"
      puts "[#{line[1].tr('*;','')} release];"
  end
end
puts

We put the input data into a file and pass that file name as the first arg to our script. Sample data could be

NSArray *titles;
NSArray *gorillas;
IBOutlet UISearchBar *searchBar;
IBOutlet UISegmentedControl *seg;
IBOutlet UITableView *tView;
BOOL firstAppear;
NSSoupPot *pot;

and we get back

jcannatti@li103-64:~/code/scripts/ruby_scripts$ ./dealloc_generate.rb data

[titles release];
[gorillas release];
[searchBar release];
[seg release];
[tView release];
[pot release];

I know that’s not that much text, but the key here is having a shell open and ready to go at all times. Then this can be a time saver.

 
1 Comment

Posted in General

 

Developing Your Programming Chops: The Way Jazz Cats Would Do It

05 Dec

I’ve been playing music almost as long as I’ve been writing code. I think I wrote my first line of BASIC about 2 years before I got my first drumset. I even spent a number of years studying classical and jazz guitar at the college level. I remember those long days in the Dana School of Music practice rooms. Four hours of practice a day is what the professors expect from you. For me, many of those hours were spent trying to learn to play jazz guitar solos by ear. Slaving away trying to get every note correct, not because I wanted to play those solos for an audience, but because that is how you learn to play like the greats.

Programmers also know that if they want to be good, they have to spend a lot of time at their craft. We tend to be more pragmatic then musicians however. Programmers spend their ‘practice’ time working on open source projects, reading O’Reilly books, or on original applications. What about trying to perfectly copy an application that you love? Sometimes programmers do this, but it usually has a practical reason. They don’t want to pay for a piece of software that they feel is overpriced, so they write it themselves. Or maybe, that software isn’t available for a certain platform that they prefer. And sometimes they even just plain think they can make something better or cheaper and take an existing product’s market.

I’m going to try something different. I am going to copy applications that I like solely for the purpose of building my chops. After the fact maybe I’ll find something useful to do with the code, but it will have no impact on what apps I copy.  The first app on my list is Stackoverflow. I’m going to use Ruby on Rails with jQuery. Again my goal here is not to have a profitable or useful product. It’s just a really good way to learn Rails, jQuery, and CSS. My iPhone development day job doesn’t gain me much experience with these tools, and I would like to get much better with them. I think copying a site like Stackoverflow feature-for-feature is a great way to learn. I’ve already easily doubled my skills in CSS. Although that only means going from about a 2/10 to a 4/10. I’ve got a pretty good clone of the front page.

CSS example

CSS example

We’ll see if when I’m done I regret my decision. Maybe my time would have been better spent fixing that bug in GIMP on OS  X, or starting a company. Who knows?

 
No Comments

Posted in General