Saturday, September 4, 2010

Objective C basic points to remember


To learn development in mac technology you need to understand the Objective C programming language, if you are aware of oops concept and a little bit of programming then you can understand this language. Before beginning with objective c you should know a few concepts of objective c.

Objective C is object oriented programming in C.

Apple has a neat and clean developer documentation which will guide you through all the    methods and properties that a particular class has.

Objective C is a mixture of small talk and ansi C.

In Objective C (OC) a class is made up of two files one is called as “YourClassname.h” which deals with the declaration part and the other is called as “YourClassname.m” which deals with the implementation part.

-   You declare all the methods and properties that the class will be containing in the .h part and will implement all those methods in the .m part.

Static methods are declared with the help of + sign and non static method are declared with the help of – sign. Example

+ (void)staticmethodName;      //declaration of static method
-(void)nonstaticmethodName;   //declaration of non static method



Both the .h and .m files together make one class.

You declare a class with the help of the directive @interface.


You implement the methods declared in the .h file in the .m file with the help of @implementation directive.


All classes in OC inherit from a super class named NSObject.


NSObject is the most important class in objective c and it is base class of all the classes in objective c.


To create object of a class in OC you need two methods “alloc” which is used to allocate memory for your class and “init” which is used to initialize the memory allocated for the class with the help of the alloc method.


Init method is called as the constructor in objective c.


If you declare a property then there is no need to add getter and setter method for them they are automatically added to your code if you use the @synthesize keyword.


If you don’t use @synthesize keyword then the property created by you will not work.


If you are creating a new project and have the Automatic reference counting (ARC) enabled then you don’t need to worry about memory management and all the dirty work will be taken care by the system itself.


ARC requires strong reference and not weak reference.



If you are not using the ARC mode then you have to manually release all the objects that you have created in each and every class or in each and every function with the help of release keyword. 

Foundation framework is the framework where all the basic data types reside.


If you want to call a function then you have to use the rectangular brackets.


[ yourObjectName methodName ]; // calling object method
[ yourClassName methodName ]; // calling static method








If you are not aware what type of data will a variable be holding then in that case you can declare the data type of that variable as “id” which is capable of holding any type of data.


If you want to add parameter to a function then you have to use the colon.

- (void)printSomething:(NSString*)uservalue;    //function with 1 para
- (void)printUserName:(NSString*)username andLastName:(NSString*)lastname;           //function with 2 para
- (void)printUserName:(NSString*)username andLastName:(NSString*)lastname anduserAge:(int)userage;  //function with 3 parameter



@selector is the directive which is used to call methods of objects.


@protocol is the directive which is used to create protocols for classes, protocols are certain set of rules that each and every class has to follow and not all the methods are compulsory, say if you declare 5 protocol method then you can implement either 3 or 2 or all protocol method.


The self keyword in Objective C points to the memory address of the current class and hence it is also called as a reference or pointer to the current class.


Delegates are called as event handling methods they are called only when an event is being triggered by a particular class or a UI control.


Datasource methods are those methods which arrange the data in the UI control with which the user interact. The source of data can be anything like an array, string, set, dictionary, database etc, and data is arranged in the UI controls as per the code written by the developer in these datasource methods.


- When you create a new project in iPhone then there are 3 frameworks which are added by default:


1. UIKitFramework: This framework contains the header files (Classes) related to the User Interface used to design the screens of the application.

2. CoreGraphics Framework: This framework contains the header files (Classes) related to all the graphical work in your application like for example if you want to give frame or a particular design to any of your UI controls then methods from core graphics framework are used.

3. Foundation Framework: This framework contains header files (Classes) related to the basic datatypes that are used in the project.

Plist: These are called as the property list, by default when you create a project then there is a project plist which is added in your application, the purpose of this plist is to provide support and allow several system level permissions to your application.

You can also add your own custom plist but that would not contain any system related permissions it would only contain the static data that you would wish to display or you may store any app related data.
Data is accessed from the plist through KVC (key value coding) pattern.


SQLITE: If you would wish to save huge data locally then you would need a database, sqlite is a lite version of the sql database and can do this task for you.

That would be it for now I hope by reading all the above data you would get a fair idea about objective C, if there is any point above that you would like me to elaborate a bit then feel free to contact me via mail or comments.


You may download the PDF version of this post from here, and if you have any sort of queries then feel free to contact me via mail or enter your queries as comments.

Until then happy iCoding and have a great day.

1 comment: