Monday, October 12, 2009

C# vs Objective C

Got a chance to mess with Objective C and Cocoa framework. Following are some typical thoughts when comparing with C# and .NET framework

• In Objective C keyword "self" is used instead of "this" of C#
• Memory allocation happens through "alloc", there is no "New" keyword in Objective C
• There is no Garbage Collector to collect objects out of scope, proper release/disposal of resources is essential in Objective C.
• Header files are imported instead of include (i.e #import statements are used instead of traditional #include statements)
• The spec of a class is divided in two parts interface and implementation, interface portion contains declaration class, variables and related methods. This interface normally lives in .h files
• Implementation portion has the actual code for the methods declared in interfaces. This implementation normally stays in .m files. So all classes are like an C# interface and its implementation, declaration and actual code is segregated.
• A class declaration starts with @interface compiler directive and ends with @end directive.
• Object references are pointers. Objective C supports both strongly typing and weak typing. Strong typing always happen with pointer declaration
• There are two types of methods, Instance methods & Class methods, Instance methods require an instance of the class to initiate the call and Class methods are much similar to Static methods in C#
• An instance method is marked with the dash(-) as prefix before the return type and signature of the method.for e.g, -(void)Insertsome:id(anobject)
• An Class method is marked with the plus(+) as prefix before the return type and signature of the method.for e.g, +(void)Insertsome:id(anobject)
• NULL in C# becomes nil in Obj C
• Calling a method is known as messaging,a message is the method signature and its parameters. All messages are enclosed in square brackets [ and ]
• A property declaration starts with @property directive followed by data type and property name.( Eg:- @property BOOL flag;)
• @synthesize compiler directive is used to instruct the compiler to generate methods according to the specification in the declaration
• C# Interfaces are called as Protocols in Objective C
• .XIB files are used to store UI related settings, i.e., if we design a form using the IDE that will be stored as a xib file. In Apple world people still use the legacy name .nib file(that is the legacy name still in use)
• In Objective C a solution is called as a Bundle.

1 comment:

Anonymous said...

Interesting Aravind.
Thanks,
-Yash