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.
Method signatures
A method signature with no arguments in Objective-C looks like:
1
|
|
The -
mark at the beginning indicates this is an instance method; class methods are indicated with a +
. The (BOOL)
is the type of the method’s value (here, a boolean).
Calling methods
In Objective-C, methods are called using infix notation. The Ruby
1
|
|
is equivalent to
1
|
|
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: tableView:didSelectRowAtIndexPath
and tableView:heightForRowAtIndexPath
.
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:
1
|
|
We’d call this method in Ruby by:
1
|
|
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.