Monday, July 13, 2009

Objective C part 1

What is ObjectiveC:
Objective-C is a reflective, object-oriented programming language, which adds Smalltalk-style messaging to the C programming language.
ObjectiveC is subset of C?
No. Objective-C is a very thin layer on top of C, and moreover is a strict superset of C. That is, it is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class.
What does it mean by "Smalltalk-style messaging" in ObjectiveC?
Objective-C derives its object syntax from Smalltalk. All of the syntax for non-object-oriented operations (including primitive variables, preprocessing, expressions, function declarations, and function calls) is identical to that of C, while the syntax for object-oriented features is an implementation of Smalltalk-style messaging.
Explain Messages in ObjectiveC?
Messages are central concept in ObjectiveC. The Objective-C model of object-oriented programming is based on sending messages to object instances.
In Objective-C one does not call a method; one sends a message. There is no concept of called method being executed. In ObjectiveC, the message sent to an object is executed! In fact, the calling object just sends method NAME to the called object. The called object received the method NAME at runtime, then figures out what to do (at runtime).
This means there is no type checking(the signatures of called method should match the call from calling object) at compile time.
And there is no guarantee that the receiving object will respond to any message sent to it; it may just ignore it(and send back null as response).
Explain Message syntax:
[obj message:parameter];
message is being sent with parameter(s) to the obj(receiving object).
Any quick advantage/disadvantage with messages:
Advantage: ObjectiveC allows messages to go unimplemented - for example, a collection of objects may all be sent a message, of which the programmer knows only some will respond, without fear of producing runtime errors. eg. The Cocoa platform takes advantage of this, as all objects in a Cocoa application are sent the awakeFromNib: message as the application launches, to which objects may respond by executing any initialisation required at launch.
Disadvantage:
Due to the overhead of interpreting the messages, an Objective-C message takes at best over three times as long as a C++ virtual method call. What comparing times with C++ virtual method call: because just like messages need not have implementation in ObjC, C++ virtual methods need not have implementation(if it is called, it needs implementation; but one can only declare virtual method, never implement it and never call it).

Let's talk interface, implementation, instantiation:

Inteface:
@interface classname : superclassname {
// instance variables
}
+classMethod1;
+(return_type)classMethod2;
+(return_type)classMethod3:(param1_type)parameter_varName;

-(return_type)instanceMethod1:(param1_type)param1_varName :(param2_type)param2_varName;
-(return_type)instanceMethod2WithParameter:(param1_type)param1_varName andOtherParameter:(param2_type)param2_varName;
@end

Plus signs denote class methods(aka static), minus signs denote instance methods. Class methods have no access to instance variables.
Note that instanceMethod2WithParameter demonstrates Objective C's named parameter capability for which there is no direct equivalent in C/C++. Todo: explain this.
Return types can be any standard C type, a pointer to a generic Objective-C object, or a pointer to a specific type of object such as NSArray *, NSImage *, or NSString *. The default return type is the generic Objective-C type id.
Todo: explain this.

Method arguments begin with a colon followed by the expected argument type in parentheses followed by the argument name. In some cases (e.g. when writing system APIs) it is useful to add descriptive text before each parameter.

-(void) setRange:(int)start :(int)end;
-(void) importDocumentWithName:(NSString *)name withSpecifiedPreferences:(Preferences *)prefs beforePage:(int)insertPage;

http://en.wikipedia.org/wiki/Objective-C

No comments: