Tuesday, 2 July 2013

Write Objective C code for absolute beginner (Quick Guide)

Objective C is basically object oriented C, its an extend C programming language. So, it has the basic syntax as C, are the primitive types, structures, functions, control flows (if, else, for, ...), pointers, and libraries.

What objective C adds to C ---> Defining new class, instance methods, methods invocations, automatic synthesizing of accessor methods, static and dynamic typing, blocks plus protocols and categories.

Lets start with classes and objects, a class encapsulates the data and defines actions to operate on that data and object is run time instance of the class.

Class extensions in objective C:
  • .h is the header file.
  • .m is the implementation file.
  • .mm is the implementation file if you are using C++ in your code as well.
 .h file:

@interface className : parentClassName

{
    // Member variables

}

 //method declarations

@end





.m file: 

// #import directive is like #include directive in C the difference is that #import directive makes sure // that the same header file never included more than once.

#import className.h

@implementation className
{
   

}

Objective C allows both static and dynamic typing:

className *object1;  // Static typing
id       object2;  // Dynamic typing
NSString *myName;  // static typing
 
 
Methods in objective C ----> Instance Methods and Class Methods
For instance method you need to create an instance of the class first then use the method. But Class Method dose not requires an instance of the class to be receiver of the message.

Instance method declaration is preceded by a minus sign (-). For Class Methods, its a plus sign (+).
 
written by: infinitywebseo 

No comments:

Post a Comment