If you’re a Rails developer coming to RubyMotion with no Objective-C experience, you’ll still need to be able read enough Objective-C to follow examples from the community. Even with Swift on the horizon, the reams of Objective-C examples floating around the Internet and books aren’t going anywhere and can be valuable to understand.
This doesn’t aim to be a comprehensive guide to Objective-C, but rather to get you just enough information to visually parse example code.
A method signature with no arguments in Objective-C looks like:
- mark at the beginning indicates this is an instance method; class methods are indicated with a
(BOOL) is the type of the method’s value (here, a boolean).
In Objective-C, methods are called using infix notation. The Ruby
is equivalent to
Methods with named arguments
Objective-C methods can have named arguments. This UITableViewDelegate methods:
1 2 3
Become the following Ruby methods:
1 2 3 4 5
Note that these named parameters are actually a part of the method’s name; that is, different named parameters constitute a different method. The method names have the parameters separated by colons, like:
This looks a bit funny when translated into Ruby, as a first glance would seem to indicate you’ve defined the same method twice. These methods are called just like you’d call methods with named parameters (in Ruby 2.0 and up) or optional hash arguments. For example, a UIViewController implements:
We’d call this method in Ruby by:
Go rake, young Rubyist!
From here, you can dig deeper into Objective-C with Apple’s language docs, but you should now know enough to grok most of the examples you’ll run across in the wild.