I started learning Objective C 2.0 (ObjC) – the primary programming language used to write Apple and iPhone applications – a few days ago after seeing the Stanford iPhone Development Course on iTunes U. I really recommend watching the lectures in this course as a podcast and making the assignments which are publicly available on the course website.
I always thought ObjC was crap and hard to understand but after a few hours of playing with it, it is actually quite easy. Here is a list of code examples for ruby developers.
The most important thing
The most important thing you should know is how to use ObjC as an Object Oriented Programming language. ObjC has everything from classes to instances to super- and subsetting classes, from class methods to instance methods and variables. The only thing you need to know is how to call them.
ObjC uses the following syntax for this:
[object message:first_argument andSecondArgument:second_argument]
Here, “object” is the object on which you want to call the function “message”. After that is a colon (:) and a comma-seperated list of the arguments. In fact the [ ] (square brackets) are the “.” and “::” in Ruby or “->” and “::” in PHP.
Assigning strings
my_string = "Hello, world!"
translates to
NSString *myString = [NSString stringFromString @"Hello, world!"]
or more convenient:
NSString *myString = @"Hello, world!"
Creating objects and calling instance methods
person = Person.new
person.walk(10); # Let's a person walk by 10
# metres by calling the "walk"
# method on the instance of person
translates to
Person *person = [[Person alloc] init]
[person walk:10] // Given that 10 or the variable in this place
// is an integer. In this case it's the integer 10
Arrays and hashes
my_hash = {'key' => 'value', 'key2' => person}
my_array = ['value', person]
translates to
NSDictionary *myDictionary = [NSDictionary
dictionaryWithObjectsAndKeys:@"value", @"key", person, @"key2", nil]
NSArray *myArray = [NSArray arrayWithObjects:@"value", person, nil]
/* Notice that the "lists" when initializing the dictionary (hash) and the array
should be terminated with "nil" */
I hope this post helps you getting started with developing applications in Objective C 2.0. If you have any tips or questions, please drop a line in the comments, get to me on Twitter/FriendFeed or send me an e-mail.
No related posts.

{ 1 comment… read it below or add one }
I just changed [Person initialize] to [[Person alloc] init] in the second ObjC example. The first one does not work.