Wednesday, June 23, 2010

Chicken Pau Objective-C

Just like local saying: "If you can't buy a Big Mac, you may still settle with Chicken Pau".

Or we might as well call it "Objective C para gente pobre" (Spanish, thanks to Claudio) above title translates to "poor man's Objective-C".

What I mean by poor man's Objective-C is the development using the language which pre-dominantly using the Apple hardware and the latest Apple compiler. That the rich man's version of Objective-C.

But if you are just like me, who is still saving to buy an Apple hardware, may be this approach will be a short term cure for your addiction. We will start with something as simple as an Ubuntu 10.04 Lucid Lynx Linux box, which is far more affordable than Mac boxes. My objective was to be able to have a hands on experience with the Objective-C language, before moving to the platform where you need to invest more bucks. Practically I decided to learn the language using "poor man's Objective-C" (it's "Objective C para gente pobre" -- thanks to Claudio).

Luckily enough, the Objective-C compiler that we use are from the GNU family of compilers (gcc, cc10bj), which by far the most widely used and widely ported compiler family. It's available everywhere for free. Those guys at Apple also uses GNU compilers, which make it easier for us -- that we don't have to install another proprietary tools.

One sad thing though, the GNU compiler set doesn't fully support the Objective-C version 2 which is the case with latest Cocoa platform. But as I said, my objective was to have a hands on experience on the language, you get the feel, familiarize with it, then you can move one when you piggy bank accumulate enough to produce a bang on the fruity stuff.

Let's start. I assume you already have your Ubuntu box installed.

[sourcecode lang="shell"]
$ sudo apt-get install gcc
$ sudo apt-get install gnustep-base-runtime
$ sudo apt-get install gnustep-core-devel
$ sudo apt-get install gobjc
$ sudo apt-get install gnustep-gui-runtime
$ sudo apt-get install libgnustep-gui-dev
[/sourcecode]

Most of the time you just need to answer "Y" for the questions raised.

Let's make it official by writing a hello world application.
[sourcecode lang="c"]
#import <stdio.h>

int main(int argc, char** argv) {
printf("Hello, world!\n\n");
}
[/sourcecode]
Save the file as hello.m

Compile it using our gcc tool:
[sourcecode lang="shell"]
$ gcc hello.m -o test.out
[/sourcecode]

If it compiles successfully, try to run ti.
[sourcecode lang="shell"]
$ ./test.out
Hello, world!

[/sourcecode]

Here we go, a hello world in Objective-C!
Ok, it's a bit cheating, the only difference from the C language in the source code was only the use of import instead of the usual include

[sourcecode lang="c"]
#import <Foundation/Foundation.h>

int main(int argc, char** argv) {
NSLog(@"Hello, world!\n\n");
}
[/sourcecode]

Now, it's a bit hairy to set the compile right, but for now, just follow this:
Compile it using our gcc tool:
[sourcecode lang="shell"]
$ gcc hello.m -I /usr/include/GNUstep -fconstant-string-class=NSConstantString -lobjc -lgnustep-base -o test.out
[/sourcecode]

If it compiles successfully, try to run it.
[sourcecode lang="shell"]
$ ./test.out
2010-06-23 18:25:06.816 test.out[4981] Hello, world!
[/sourcecode]

Hooray, now we have the development environment that runs the Cocoa Foundation framework!

A glimpse of the Objective-C langauge

The Objective-C language has had history almost as long as C++. The languages was developed in roughly the same time (Stroustrup came with the C++ idea first, but the first implementation roughly emerged at the same period). The Objective-C has long seemed to be hidden somewhere in the secluded community while its cousin has made it to a whole bunch of commercial compilers and IDEs.

Only until recently the Objective-C shows back its teeth. Now with Apple Macbooks, Macbook Pros, Mac Minis, iMacs, iPhones, iPads, iPods devices around everywhere, the Objective-C is enjoying popularity as it has never before -- to the extent that now Apple is the largest technology company in term of market cap.

The language has been with the NeXT, a company founded by Steve Jobs after leaving Apple, and the company that brought back Steve Jobs to Apple. Having its popularity at its peak, it brought about the whole tools it's been using all those time.

I have always wanted to learn this language. I remembered it's been there for very long. I saw things comp.lang.objective-c group in the Usenet. But those day when my campus' bandwidth was still very limited, and the only few NeXT workstations are out of reach, it's difficult to get access to this language and platform.

A short look at the code revealed that it is a derivative of C language that one to make sure that the message passing between objects are very clear. I heard this was an influenced by Smalltalk, which I think make senses. I often encounter a lot with people who started learning and developing C++ applications; when they talk about object's "methods", they still call them "functions". They still ask "how to call this object's function" when the correct question is "how to invoke this object's method". That's not the case when you learn the Objective-C language. "invoking a method" also called "sending message" in Object Oriented paradigm (I think it's from Message Sequence Chart, the predecessor of UML's Sequence Diagram). The Objective-C is stressing more on the latter. It must also has been influenced by Mach micro kernel design and MPI which uses message passing instead of passing a shared memory location.

The good thing about thinking in a way of "sending message" or "passing message" from one object to another is, it applies well to a fine degree of parallelism. When you are running on a lot of core (as it is now), it's good to see the objects as sending messages to each other, and not invoking methods. "sending message" across objects are safe meanwhile "invoking object's methods" must consider all the thread related stuffs.

I know that in order to become a good Cocoa (the framework for Mac OS X and iPhone apps) application developers, we'll need to learn the language well. The language is used by most of the GUI frameworks in Apple platforms. While some of the syntax is just resemble what we have in C language, the Objective-C has also departed from C language in so many ways. Some of the syntax looks very weird, especially when you are from C++ and Java background like me.
It must also has been influenced by Mach microkernel design which uses message passing instead of passing a shared memory location.


[sourcecode lang="c"]
NSString *myStr = [[NSString alloc] init];
[/sourcecode]

I don't know how weird it is for you, but this is the syntax of allocating memory for the NSString object (simply by sending "alloc" message), then sending "init" message to the newly allocated object.

Friday, June 18, 2010

Now running on Wordpress 3.0

When I was editing one of the posts, I got a notification to upgrade to Wordpress 3.0. The upgrade was seamless (at least I didn't notice any glitches).

I wish some other application will be able to do the same thing when upgrading.

Great job, Wordpress team!

Wednesday, June 16, 2010

Twitter overloaded again...

Yesterday evening I opened Twitter site after not checking it for quite some time. It was over loaded... It says "Twitter is over capacity."

Some friend says last time they notice this happens when the Dow Jones Industrial Average (DJIA) dropped for a while.The market dip. It seems that the stock market users like to tweet.

What was causing yesterday's over capacity?

World Cup match?

Monday, June 14, 2010

Mercurial Server 1.0.1 Installation on Ubuntu

When we are working using Subversion, we can expose the Subversion repository using svnserve (open the default port 3690 and svn:// protocol) or leverage the Apache WebDav to expose your Subversion service in HTTP (the web protocol -- I've always tempted to write it as HTTP Protocol which is incorrect as the last P letter stands for 'protocol').

And of course if you are just as lazy as I, you will use the CollabNet Subversion or Edgewall Trac.

Mercurial Server from Lshift is one way to achieve similar thing. Though it is named Mercurial Server, it is not a server in traditional client/server perspective. It leverages the SSH authentication to make it like a central repository where multiple user can access and serves the same purpose.

Surprisingly, it is very easy to install in an Ubuntu box.

[sourcecode lang="shell"]
$ sudo apt-get install mercurial-server
[/sourcecode]

Gotcha! You have it now.
With this Mercurial Server, you can access the server using:
hg clone ssh://<ssh-user>@<host-name-or-ip-address>[:<ssh-port-num>]/<folder>

[sourcecode lang="shell"]
$ hg clone ssh://mercurial@192.168.0.1/projects
[/sourcecode]

Saturday, June 12, 2010

Attending Singapore Google Technology Group Meetup

 

Today I attended this Singapore Google Technology User Group Meeting. It's my first time to attend such a Google meetings. Some of previous meet ups had always fallen on the wrong days -- on the day that I have something else to do. Today I am glad to be able to make it, attended the meeting, and Wesley Chun, was the speaker. He presented 2 sessions, one about Python and the other one about Google App Engine.

This guy's a hardcore Python guy, wrote the Core Python series of book, and was a presenter in the PyCon Asia Pacific 2010, Singapore -- an event that has been held last 3 days. Awesome in depth knowledge, vast industrial experiences, and keep the talk flows fluent.

The rest about the meeting can be found here: http://www.sg-gtug.org/2010/05/sg-gtug-special-tutorial-session-june.html