Monday, June 30, 2008

Happy Objective-C

I love Obj-C :D

What I learnt in today's reading:

NSObject - superclass of everything except your own root class [way difficult]

Classes as the object factory since each object is an instance of a class, therefore it's a class object.

Some similarities between instanceof and isMemberOfClass:someClass method.
and isKindOfClass:someClass method which tells if the object we are evaluating inherits from someClass.


Static Typing (ohh pointers here we go!) Useful for the compiler to do type checking

Rectangle *myRect <- This specifies that myRect is a pointer to Rectangle class and Graphic *myRect is also valid since Rectangle inherits from Graphic, therefore it can be statically typed like that.

Class Objects
The body of our object, meaning the method implementations, name of the class and superclasses, instance variables, declarations of methods and their return types.

Class can be typed id or to the Class data type

id aClass = [anObject class] or Class aClass = [anObject class]

Main function: to create new instances.

id myRect;
myRect = [Rectangle alloc];

what alloc method does is to dynamically allocate memory for the new object's instance variable and initializes them all to 0. But we've got to initialize even properly, not just to 0. ( there we go!!)

What we do to initialize is to surround the alloc with init, like this:

id myRect;
myRect = [[Rectangle alloc] init];

Just then myRect would be able to start interacting (e.g. receive messages). The structure is:
alloc returns an instance of Rectangle
init is performed by that instance to set its initial state

I knew I was inside the matrix!! There is a NSMatrix object!!

Messages rock! Look at this:

[myMatrix setCellClass:[NSButtonCell class]];
so what does this mean? (you can answer, you've learnt all the way with me!)
No clue? Well here it is...
NSButtonCell class returns what? A Class or an Id type therefore that is sent to setCellClass which is a setter to myMatrix NSMatrix object (previously initialized). Clear? No? One more time:
myMatrix is an object (instance) of NSMatrix. The CellClass that will be filling that myMatrix object is being defined by the Class passed to the setter method, NSButtonCell.

Class names can only be used in two ways. As part of a message (receiver) or as a type name for a kind of object. Since I've already explained this you should know it.

One new thing would be that if you don't know the class name at compile time but you will at runtime then use NSString *className
and create this monster:

[anObject isKindOfClass:NSClassFromString(className)]
So what does the last statement means?

That's it for today and you've got homework!!

No comments: