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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def found_location(loc) | |
coord = loc.coordinate | |
point = BNRMapPoint.new(coord, locationTitleField.text) | |
worldView.addAnnotation(point) | |
# ... some cleanup code here | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BNRMapPoint | |
attr_reader :coordinate, :title | |
def initialize(coordinate, title) | |
@coordinate, @title = coordinate, title | |
end | |
end |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BNRMapPoint | |
def initialize(coordinate, title) | |
@coordinate, @title = coordinate, title | |
end | |
def coordinate; @coordinate; end | |
def title; @title; end | |
end |