NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.
This tutorial assume that you have some experience with C++ and concepts of Programming Languages.
Basic differences between objective-C and C++:
- Keywords begin with the @ character, for example: @class, @try, @catch, etc.
- Boolean type is BOOL (instead of bool as in C++). They can be set as YES or NO
- Every object is of type id
- Classes:
- They are objects.
- They are instances of a meta-class (root class).
- This means that they can be dynamically managed
- You can create instances base on the name class
- Add classes at run-time
- Ask methods from the class
- Equivalent to C++ Run-Time Type Information (RTTI) but more powerful and with out the lack of portability issues related with RTTI.
- Objective-C defines a root class, NSObject. Every new class should be derived from this class.
- Since classes are meta-class instances (objects), it is possible to declare a pointer to them.
- nil is the equivalent of NULL in C++ for an object pointer; however, do not interchange nil and NULL
- The equivalent of nil for a class pointer is Nil.
- Interface code of a class goes in .h files
- Implementation code of a class goes in .m files
- Implementation code of a class in Objective-C/C++ goes in .mm files
- Directive #import replace directive #include.
- The directive #importinclude already the compilation guards normally used in C++, example:
#ifdef COMPILATION_GUARD_H #define COMPILATION_GUARD_H ... /* code */ ... #endif // COMPILATION_GUARD_H
- The directive #importinclude already the compilation guards normally used in C++, example:
- As standard, all class names begin with the prefix NS, example: NSString
- Calling a method is done by using the following syntax:
[my_object do_something]
instead of using this syntax as in C++:
my_object.do_something();
- Objects:
- All of them should be of type NSObject
- Every pointer to an object should be declared as NSObject*
- Using type id is a short way to declare a pointer to an object plus it provides with a dynamic-type checking
- Null object pointers should be set to nil (instead of NULL as in C++)
- Instance data are the equivalent to attributes in C++.
- Methods are the equivalent to member functions in C++.
- Instances data and Methods cannot be mixed as they are in C++
- Attributes are declared in braces while their implementation lies inside the @implementation block.
- This allow the implementation of some methods without being exposed in the interface
- The prefix “–” indicate instance methods while the prefix “+” indicate a class method (staticin C++).
- These prefixes should not be confused with private and public keywords of C++.
- These prefixes have nothing to do with UML “-” and “+” symbols
- Type of parameters are enclosed in parenthesis (…)
- Parameters are separated with colons “:“
- @interface keywords is the equivalent to the class keyword in C++
- Do not confuse with @class keyword which is used only for forward declarations
- Example:
- In C++:
class Base{ private: int value; public: int getValue(); void setValue(int new_value); }; ... int Base::getValue(){ return value; } void Base::setValue(int new_value){ value = new_value; }
- In Objective-C:
@interface Base : NSObject{ int value; } -(int) getValue; -(void) setValueint) new_value; @end @implementation Base -(int) getValue { return value; } -(void) setValueint) new_value{ value = new_value;} @end
- In C++:
- Attributes are declared in braces while their implementation lies inside the @implementation block.
- Forward declarations in Objective-C are done by using the keyword @class
- @protocol keyword can be also being used
- Example:
- Forward declaration in C++:
// car.h #ifndef CARLSTEIN_CAR_H #define CARLSTEIN_CAR_H class Motor; // Forward declaration class Car{ private: Motor *motor; public: void start(); }; void Car::start() { ... } #endif // CARLSTEIN_CAR_H
- Forward declaration in Objective-C:
// car.h @class NSMotor; // Forward declaration @interface NSCar : NSObject{ NSMotor *motor; } -(void) start; @end @implementation NSCar -(void) start {...} @end
- Forward declaration in C++:
- Public, private and protected:
- In C++, attributes and methods can be public, private (default mode), and protected.
- In Objective-C, methods can only be public.
- However, it is possible to ‘mimic’ the private mode for methods by declaring these methods inside @implementation without declaring them inside @interface.
- Using this trick make the methods less exposed but they can still being called.
- However, it is possible to ‘mimic’ the private mode for methods by declaring these methods inside @implementation without declaring them inside @interface.
- Instance data can be public, private, and protected (default mode).
- Inheritance can only be public, it cannot be tagged as public, private, or protected.
- Example:
- In C++:
class Car{ private: Motor *motor; void start_computer(); public: Accessory *accessory; void start(); protected: ExternalSensor *ext_sensor; void check_sensors(int number, char type); };
- In Objective-C:
@interface Car : NSObject { @private: NSMotor *motor; @public: NSAccesory *accessory; @protected: ExternalSensor *ext_sensor; } -(void) start_computer; -(void) start; -(void) check_sensors : (int) number : (char) type; @end
- In C++:
- As you can see in the previous example:
- The return type of a method is enclosed by parenthesis (…)
- All parameters are separated by colons “:“
- “–” prefix is to indicate an instance method
- “+” prefix is to indicate a class method (static in C++)
- Remember that methods are always public
- Declaring a class data attribute such as staticin C++ is not possible; however, we can be ‘mimic’ it:
- In the implementation file, use a global variable.
- By using accessors on it, the class can use the variable with class methods or normal methods.
- In the initialize method of the class, the variable can be initialized.
- When working with prototypes, calls, instance methods and class methods have in consideration:
- The return type of a method is enclosed by parenthesis (…)
- Parameters are separated by colons “:“
- Method’s name and attribute’s name can be the same (normally this is use for creating getters)
- A label can be associated with parameters. This label is a name specified before the colon “:” and become part of the name of the method plus modifies it.
- The first parameter must not have a label since the name of the method is already the label.
- In Objective-C, we do not say that we call a method but that we send a message to the method.
- Example:
- In C++:
//prototype class Base{ ... insertObjectAt(void *, size_t); } ... void Base::insertObjectAt(void *obj, size_t index) ... Base shelf; shelf.insertObjectAt(new_object, 9);
- In Objective-C:
- Without using label:
@interface Base : NSObject{ ... } ... //prototype -(void) insertObjectAtid)objsize_t)index @end ... Base shelf; [shelf insertObjectAt:new_object:9];
- With using label:
@instance Base : NSObject{ ... } ... //prototype. -(void) insertObjectid)obj atIndexsize_t)index @end ... Base shelf; // This is easier to read [shelf insertObject:new_object atIndex:9]; // This WORKS! // This fails plus it is not easier to read [shelf insertObject:new_object:9]; // This FAILS!
- Without using label:
- In C++:
- self keyword is the equivalent of thiskeyword of C++
- Unlike this keyword in C++, self is not a real keyword and its value can be changed (useful in constructors).
- self is a hidden parameter which each method receives. The value of this hidden parameter is normally the current object.
- Example:
- In C++:
class Point { private: int x; int y; public: void setInterceptY(int new_y); }; void Point::setInterceptY(int new_y) { x = 1; int y; ... y = new_y >> 1; // local y ... this->y = y; // object's y }
- In Objective-C:
@interface Point : NSObject { int x; int y; } -(void) setInterceptY : (int) new_y; @end @implementation Point -(void) setInterceptY : (int) new_y { x = 1; int y; ... y = new_y >> 1; // local y ... self->y = y; // object's y } @end
© 2011, Alejandro G. Carlstein Ramos Mejia. All rights reserved.