Wednesday, 31 December 2008

Introduction to Objects - part II

First of all a happy 2009 for all of you !

In a previous blogmessage I explained a bit the concepts of objects.
Now another important concept is the way how objects communicate with each other.

The communication between objects happens by sending messages. If for example an object A wants to retrieve something from object B then A will have to send a message to B that B understands.

It looks a bit abstract but later on I'll show how that works in Objective-C.

Now their is a difference in a message and a method invocation. As already explaned an instance of class can be part of a whole class tree structure and as such a message can be implemented in different ways (=methods).
For example take a number of objects which are instances of the classes Point, Circle, Rectangle. To those objects you can send the message 'draw' , the actual method that will be invoked will be different for each object.

Note that in Objective-C you don't have to care about that, its the compiler/system that ensures that the right method is invoked.

Oke, this is enough theory, in a next message we'll start with some programming

Tuesday, 30 December 2008

Introduction to Objects

Before we can start programming in Objective-C, I'll first give a short explanation on the core fundamentals of OO.
You have first to think differently, you should remember only 1 concept :
"Everything is an object"
Let's look in the real world , you see all kinds of people in the street; you have Jan,Joe,Liz and Peter for example. Each of these people have characteristics (like length, weight, haircolor etc) and they do something (walking, reading, eating etc) eg they have a behaviour

So to summarize : objects have characteristics and behaviour.

Now to go one step further :
Jan, Joe, Liz and Peter are sharing the same types of characteristics and behaviour. We say that they belong to the same Class.

We can call this class Human. So Jan, Joe, Liz and Peter are all instances from the Class Human.
You can consider classes as factories out of which you instantiate objects (objects of a certain class).
Now you see that the class Human is a fat class, with lot of characteristics (or attributes) and behaviour (or functions, methods).
So sometimes you should refactor a class, in the case of the class Human we could factor out specific behaviour (and attributes) for man and woman .
This specific behaviour&attributes we organize in the classes Male and Female. These new classes are subclasses from Human. 

The class Human is what we call the superclass of Male and Female.

Next time we will see how objects communicate with each other (another important concept of OO) and the finally how all this is implemented in Objective-C

Monday, 29 December 2008

Objective-C and Smalltalk

In a previous message I promised to discuss a more complex Cocoa/Obj-C application.

But afterwards I've realized that we should better discuss first of how to build a proper OO application.
To do this I will compare Obj-C with Smalltalk.

To my surprise Obj-C looks a lot like Smalltalk , but with a lot more syntax sugar.

So watch this blog to get a full understanding of OO, Obj-C and why Smalltalk is still the best OO-language in the world :-) :-)

Sunday, 28 December 2008

HelloWorld ?!? That was something

In the previous blog their where a few oddies that need some explanation :

In a typical Cocoa application you have what they call outlets and actions. And a view (=window) is connected to a controller.

Now an action is something you connected from the view to the controller. 
While an outlet is something you connect from the controller to the view.

A textfield, a list and so on are typically outlets because the controller will fill them. Buttons, comboboxes etc are more action oriented.

Something special is also the awakeFromNib function. As you see , I've added the code to initialize the textfield with HelloWorld in this function.

The awakeFromNib function can be seen as a placeholder where you can add initialization code to your GUI after the GUI is loaded. 

In a next blogmessage , I will publish a more complex application (well yeah , complex for the beginner)

For starters...

Anybody who starts with programming makes always the same first little program : the 'Hello World' application.

Well lets see how this works in Objective-C.

First start Xcode.
It's always a good practice to put your projects and builds in seperate directories. You can change this in XCode>Preferences>Building. 
Change the Build Products directory and the Intermediate Build directory.

If you done that , you can create a new Project (via XCode > File > New Project ) the project assistant will popup, choose 'Cocoa application'  and  call your new project HelloWorldProject.

As soon as you did that a project browser will show up (see below). 

Surprise ! You see a lot of files here, all this is generated by XCode.

Don't worry about them yet.

Let's first build our little HelloWorld application.

Start by double clicking the MainMenu.nib file, this will open the Interface Builder. When the interface builder, again a number of windows will open :
1. A blank window in which you'll design the UI
2. A palette with all types of UI controls (Cocoa -controls)
3. A menu-bar
4. The mainmenu nib itselfs

Start with dragging a text field from the Cocoa-controls to the blank menu (the text field controls can be found by clicking the third item on the menu bar of the Cocoa-controls window).

Now the first part of the helloworld application is done.

The second part consists of the creation of a controller. Now here we need some theory.
In fact what we want is an implementation of the Model-View-Controller design pattern.

In our case we have built a View with the Interface Builder and now we need to put some logic behind to do something useful. This logic we will put in a controller.

The setup of a controller is again pretty straightforward :

In the Mainmenu window you select 'classes' and look for the class NSObject (see screenshot below) 

Now up in the menu you select 'Classes>Subclass NSObject'. A new class will appear , you can rename it to HelloWorldController.
Now we need to create the files in which we will write Objective-C code.
Therefore you you go to the menu and select 'Classes>Create Files for HelloWorldController'. A popup will appear, let everything default and press the 'choose' button.

The next thing you must do is to instantiate that class (otherwise your newly created view won't work).

To do this you first select your newly created controller (HelloWorldController) and then you select from the menu 'Classes>Instantiate HelloWorldController'.

Voila we are almost there ! Now we need some coding.

A first step is to create an outlet in the controller class. This you do by selecting the HelloWorldController class in the class browser.
And then in the menu you select 'Classes>Add outlet to HelloWorldController'.

A popup will appear in which you can define the outlet (call it sayHello) .

Once the outlet is created we need to link it to the text field.

This is done by selecting the HelloWorldController instance in mainmenu window.
You then CTRL-Click the HelloWorldController (keep mouse button pressed) and drag to the text field (a blue line will appear). Release the mouse and a popup will appear, in this popup you see all the outlets defined (in your case only one) .

Now connect the sayHello outlet by clicking the 'connect' button.

Now we are done with interface builder, use Command-Q to quit it ( he will ask first to save all your work).

In the XCode project browser you'll see 2 new files, HelloWorldController.h and HelloWorldController.m.

In the .h file you should add the following line (between the { } ) :

IBOutlet id sayHello ;

In the .m file you should add the following method  (after the @implementation line) :

- (void) awakeFromNib
[sayHello setStringValue: @"Hello World" ];


Now build and run this application, and YESSSS you see that 'Hello World' appears



Purpose of this blog

I've created this as a tutorial for Objective-C development for Mac OSX & Cocoa.
I  will also add all kind of resources I find on the internet with regard to Objective-C .

The development environment I will use is XCode, the standard of Apple