RSS
 

Archive for November, 2009

Generating Objective-C code with Ruby: Part 1 — RGB Hex codes to UIColor

18 Nov

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.

 
No Comments

Posted in General

 

I Google People’s Names.

17 Nov

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.

 
2 Comments

Posted in General

 

O Wiki, Where Art Thou?

17 Nov

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?

 
No Comments

Posted in General