This blog entry is a work in progress. I will keep updating this as I learn more about objective-c.
Differences between Objective C and C++ / Java.
Concept | C++ / Java | Objective-C | Details |
File name extension | .h / .c / .cpp / .java | .h / .m | Declarations in .h file. Implementation in .m |
Declaring a method / message | int add(int x, int y) | - (int) add:(int)x with:(int)y | types are always enclosed in brackets |
Calling the method / sending a message | int result = obj.add(10, 12); | int result = [obj add:10 with:12]; | message name and first parameter are same |
Nesting method calls | int r = obj.add(obj.getX(), obj.getY); | int r = [obj add:[obj getX] with:[obj getY]]; | |
Formatting | All printf formats (%s, %d etc) | All C formats, and %@ | %@ prints and object's "description" |
Calling methods in parent class | super.init(); | [super init]; | |
Refering to myself / current object | this.add(10, 12); | [self add:12 with:12]; |
Other things
- Type (id) represents current class. No need to specify name of the class
- NULL: the keyword "nil" represents a null value.
- Message passing is nil safe. nil elements do not core dump / throw a NullPointerException. Data members are auto-initialized to nil
- All method / message binding in Objective-C is dynamic. Classes maintain a list of messages that will be accepted at runtime. All other messages are passed on to parent. This processing happens at run time.
- For Class level messages, replace the "-" with "+" as the first character of the message declaration and implementation
No comments:
Post a Comment