Generating Objective-C Code With Ruby: Part 2 — Dealloc December 16, 2009 at 8:28 pm

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.

Developing Your Programming Chops: The Way Jazz Cats Would Do It December 5, 2009 at 4:28 pm

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?

Generating Objective-C code with Ruby: Part 1 — RGB Hex codes to UIColor November 18, 2009 at 9:05 pm

Objective-C is a good language. I enjoy using it more than many other technologies out there. I will however, knock Obj-C for it’s verbosity. You’re going to be doing some typing. That’s for sure.

There are many people out there who are trying to get around Obj-C for iPhone development all together.  iPhone Wax lets you use Lua, Flash compiles down to an Xcode project, and Taplynx doesn’t look like it requires any code at all.

I do not think any of these technologies are a good idea for professional programmers. I am comfortable with doing my own memory management, and I don’t see a reason to add a very large dependency to your app. I do however find it helpful to attempt to generate some of these piles of code you’re going to be needing.

In this series of posts I will show you how I save time by using Ruby to generate Objective-C code for my iPhone application. As Obj-C and the Apple frameworks improve, the scripts in these post may become unnecessary, but until that time comes I’ll be keeping a terminal window open to my ~/scripts directory.

We’re going to start out really simple here. One point of excessive typing that I frequently encounter is UIColor. My designer coworker frequently presents me with hex values for colors in the app. I’d often end up with four lines of code or one totally unreadable line to get that color into the app. So here is a very basic Ruby script that I wrote to take care of this problem.

#!/usr/bin/ruby

rgb = ARGV[0]

exit if rgb.nil? or rgb.length != 6

r = rgb[0..1].to_i(16)
g = rgb[2..3].to_i(16)
b = rgb[4..5].to_i(16)

puts "\n// Hex ##{ARGV[0]} -- r:  #{r} g: #{g} b: #{b}  "
puts "[UIColor colorWithRed:#{r/255.0} green:#{g/255.0} blue:#{b/255.0} alpha:1.0];\n\n"

To use it you just pass the hex code (with no #) as the first arg.

$ ./convertRGBtoDec.rb C84EAC

and just like that you’ve got

// Hex #C84EAC -- r:  200 g: 78 b: 172
[UIColor colorWithRed:0.784313725490196 green:0.305882352941176 blue:0.674509803921569 alpha:1.0];

That’s it. Now this might seem pretty trival, but we are going to get much more involved as this series of posts goes on. Once you have a nice sized dir of these, you’ll be amazed at how useful they can be.

I Google People’s Names. November 17, 2009 at 11:41 pm

Co-workers, classmates, dates, friends, just about everyone really. But what’s surprising is how little information this reveals. Well, maybe it’s not so surprising when the cute blond from the bar’s name only reveals her facebook page, but what about people you know create stuff. Why does that awesome Rails developer from work’s name not reveal all the great sites he’s done. What about the guitar player from the coffee shop who always plays wonderfully. I know their names why can’t I find all the cool stuff they do.

Well, the reason is simple. Most of you are probably thinking the reason is totally obvious, but that’s because you’re guilty of it as well. The great guitar player uses the name FenderDude420 for everything he does on the web. The Rails programmer’s stackoverflow screen name is PuddingSoup……What the hell?

It’s not that I don’t appreciate a good nickname, but the Internet is now the main source of communication in the world and you want people to be able to find you. Anyone who only uses their first name and last initial on Facebook, or doesn’t put their name on their blog, or uses a nickname for any account on any site is defeating the purpose.

That’s right I said it. They are defeating the purpose of the Internet.

Either they are not proud of what they say and do or they are paranoid.

O Wiki, Where Art Thou? at 11:39 pm

Online mapping applications are a pretty huge thing these days. A sixteen year old girl recently asked me how we got directions in the “Olden Days”. Keep in mind that i’m 24 years old and don’t consider any part of my lifetime to be in the “Olden Days”. That aside, I listed the steps to her as so:

  • Get your phone book.
  • Use the YellowPages to get the phone number of the place you want to go.
  • Call them and ask for directions
  • Write down what they say.

Her: “You had to have a phonebook in your house?
Me: “…………………yeah”

It got even more hilarious when I tried to explain how maps were used.

I said “You would get the grid coordinates for the street from the chart in the back, You know like b-4.”
She blinked repeatedly then smiled.

My point here is that Googlemaps(and similar webapps) are a part of most people’s lives. This technology is here to stay and is going to keep advancing. One of the newest features is centered around Wikipedia’s GeoTags.

A large number of Wiki articles now display the geographic coordinates in the top right corner of the page. This obviously represents the location of whatever the article is about.

GoogleMaps has implemented this into their app through what they call the Wikipedia Layer. It displays a Wiki logo for all articles GeoTagged within the view of the map. Clicking the logo shows a description of the article.

Aside from “Wow, that’s cool.” The first thing i thought when i saw this was “How can I make my apps do this?”

I came up with two useful methods.

Parsing the Wikipedia does not seem like the most difficult thing in the world to me, but Geonames provides a very easy to use java api. I’m talking this easy:

Double lat = 41.2856;

Double lon = -81.4011;
List <WikipediaArticle> list = WebService.findNearbyWikipedia(lat,lon,”en”);

Now there is only one problem with this. GeoNames will only let you hit the webservice 50,000 times a day from a given ip address. After that it will throw an exception. Let’s say we’re trying to write an iPhone app that utilizes this data. If there is a chance you will eventually begin hitting this limit I would do this.

  • Create a Servlet that essentially is a wrapper on the Geonames service. This, first and foremost allows us to keep most of the task-specific processing off the the mobile device and on the server. Secondly, if we begin hitting the the 50,000 request limit, we can switch to parsing the Wikipedia data our self without any change to the client code. Lastly, if we decide to port this app to devices other than the iPhone(i.e. Android, Blackberry, Palm, etc) this will slightly ease the process.

    In Conclusion:
    It’s 12:03 AM, do you know where your Wiki is?