Objective-C Experience

I still very new to Objective-C, but here I would like to share a some interesting thing about this language.

Objective-C is a language based upon C, with a few additions that make it a complete, object-oriented language. Objective-C is interesting because the emphasis on simplicity (this language is inspired by smalltalk) and flexibility. But like C, flexibility allows us to perform great trick while forcing programmer to introduce his own convention to prevent error. Yeah, there is fun in danger, isn’t it?

Classes are object

Each class is an instance of meta-class created and managed by run-time. So we can define class, put them in the argument, put them in the collection and so on. If we want to create an object, so send a message to them to built it.

Dynamic and static typing

Dynamic typing:

id dynamicTyping;

Static typing:

NSString* staticTyping;

Self explained

Categories

One of the most frustrating aspects of using classes written by other people is that these classes often don’t contain some key functionality that you need. One way to add this functionality (assuming you don’t have the source to the original class, or don’t want to change it for some other reason), is to simply make a subclass and implement the methods you need in that class. But this creates another problem. Now you have to tell everyone else to use your particular subclass and not the original class. Or what if someone else has already subclassed this class and added different functionality. How do you combine the two subclasses?

Objective-C provides a very simple and elegant way to add functionality to an existing class using Categories. Category let us define some method and add them to some specific class (even if we don’t have the source code). I think it is extremely useful to adapt class for specific purpose. This is for example, adding a method to existing NSString class (this one is like String class at Java):

@implementation NSString (NSStringAdditions)

-(NSString *) reverseString {
// reverse string
}

@end

This makes any class that include the category header (‘NSStringAdditions’), NSString object can call the ‘reverseString’ method.

Message sending

We interact with object by sending them object. Usually, the object will directly execute the message if they have the exactly matched method. In this case, it is like invoking object’s method. But this is not the only possible outcome, as an object can choose to handle a message in other ways such as forwarding it to another object, broadcasting it to a number of objects, introspecting it and applying custom logic, etc.

Posing class

Posing class is really cool. We can have our own class become any other class, allowing us to do some work, and then forward up the inheritance chain. When you pose as, your class gets used whenever the other class would be used. For example – to peek at a textview when they’re drawn anywhere in your program

@interface TextViewPoser : NSTextView
@end // TextViewPoser

@implementation TextViewPoser

- (void) drawRect: (NSRect) bounds
{
    NSLog (@"drawRect");
    [super drawRect: bounds];

} // drawRect

@end // TextViewPoser

Then in your main.m (or somewhere convenient)

[[TextViewPoser class] poseAsClass: [NSTextView class]];

Tool chain

This is when things get a little bit ugly. There’s a ton of Makefiles out there, along with piles of bash scripts, but it is not some magic button. It is not like miraculous Ant or Maven that forged by the magical Elves. 😀

 

At last, it is a nice language to be used but Objective-C is only used to develop MacOSX or iOS app only. And I would rather choose another language for compatibility. Thank you 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s