This blog has moved to devblog.riesd.com

Wednesday, January 23, 2013

RubyMotion Crashing Samples

Recently I've been playing with RubyMotion. I worked my way through the Pragmatic Programmers book which was a good introduction to the tools and workflow of RubyMotion, but left me feeling like I still had no idea what an iOS app is made of.

I got several suggestions about the Big Nerd Ranch book. Which is a great introduction to the iOS framework. I don't know Objective-C very well, but translating the samples to Ruby is pretty simple.
Now I was on my way to making awesome iOS apps in my favorite language. I even get to leverage awesome gems to make life nicer. But there was one big hurdle I ran into: mysterious simulator crashes that left no backtrace.

Let's use an example: The WhereAmI sample application from the Big Nerd Ranch book has just enough complexity to be interesting, but is simple enough to keep in your head all at once. It basically just shows a map, finds your current position and lets you name that position.

When you enter the name and hit "Done" it sticks a pin on the map with the name you entered. The main action of the app happens in the found_location method of your view controller. A very direct translation from the Objective-C samples to the Ruby might look like this:
But when you run this app and try to name your location the app suddenly crashes with the mysterious no-backtrace, no-error message screen below.

At this point it is not at all clear what is wrong with your code. The specification for a MKAnnotation just specifies that you must have a coordinate and an optional title. So why is this crashing? Well if you rewrite the BNRMapPoint class as seen below your code will suddenly work.

What is the difference between an attr_reader and defining a method that returns an instance variable? I don't know enough about the Objective-C runtime to know, but if you are getting the mysterious crash you may want to get rid of some of your Ruby-foo and trade it for old fashioned methods.

No comments:

Post a Comment