Thursday, June 26, 2008

id, nil, pointers, messages, dynamic binding...

I dedicated some hours to read the ObjC guide by Apple.

Extremely interesting and in fact addictive. Since I don't have that much spare time I read only the Objects part. I'm missing classes and I'll finish Chapter 1.

A glimpse of what I've read:

Every object is of type id
id anObject;

nil is a null object with an id value of 0

[anObject aMethodInside] <- this stands for a message being sent to object anObject (receiver) telling that aMethodInside(message) has to be applied.

Regarding to parameters being sent inside the message for specific behavior. Suppose it's a rectangle object called myRec and a method inside it called setWidth.

[myRect setWidth:20.0];

Do you see the colon? Well that separates the values from the method. If you are wondering how to send more than just one obligatory param, this is the answer:

[myRect setOriginX: 30.0 y: 50.0];

And if we have a method which receives optional params? Perhaps a method that returns an array?

[receiver makeGroup:group, memberOne, memberTwo, memberThree];

As you can see makeGroup implementation inside the object could be the declaration of a group array that is being filled with three elements at runtime.

Dynamic binding is pretty useful because it handles polymorphism within methods. This happens because method implementation is different among objects but the method can be named the same. Therefore at runtime the behavior is not the same and results are exclusive for each object.

Fair enough, that's it for now. I'll study some more for my tomorrow's midterm.

Thanks!

edit: blog says 1:33am and as of my writing its 3:50am. I'll take a look where to change my time zone.

No comments: