I’m starting to learn Objective-C reading Apple’s documentation and it’s very interesting to see features that I love in Groovy are available to use in such old-school language (30 years old). At a first sight I thought this would be a painful language to learn without any cool feature from modern language like Ruby and Python. Also thought Apple doesn’t care about its programming language as much as the user experience, fortunately I was wrong.
The strangest thing for those who have a Java background is how methods are called it doesn’t uses dot notation. See the between a method call ‘workHard’ on an ‘employee’ object and instantiate a new instance in Java and Objective-C:
|new Employee()||[[Employee alloc] init];|
A Dynamic Language
Just like Groovy the Objective-C language defers as many decisions as it can from compile time and link time to runtime. That means among other things that in order to run a code you need not only a compiler but also a runtime system to execute the compiled code, this system is already ready to use in iPhone OS and Mac OS X.
It’s possible to declare a variable as strong or weak type, giving us tremendous flexibility.
The examples above shows how to declare variables to store objects, the first myVar reference accepts only NSString instances and the later accepts any object. The id is just like Groovy’s def keyword. Using the id means that we can send a message to that instance even if the compiler can’t guarantee it’ll work.
(Yes! I thing the word ‘id’ meaning anything isn’t good.)
Categories let’s you add method to an existing class, even if you haven’t the source. This is my favorite similaraty with Groovy I found so far I hope there’s many more to come, see a sneak peek of the syntax:
1 2 3
Protocols, the Java’s Interface
Just like Java’s interface, so no need to explain it here, except for one cool feature: we can declare optional methods. In practice when one of our classes conforms to a protocol that define options methods we aren’t obligated to implement them.
Besides the language itself Cocoa and Cocoa Touch - Apple’s framework for Mac OS X and iPhone OS respectively - are great frameworks with lots of functionalities, it’s easy to build beautiful applications. Interested in learning more about Objective-C? Take a look at this docs:
Have fun improving your programming skills.