Tuesday, December 27, 2011

Objective-C basic

Objective C
This is a primitive language used in Apple device application development (IOS/Macintosh). The base of this language is c. Objective –C is an easy to understand and object oriented programming language.  Objective – c comes with library, development tool and OOPs.
The basic 3 parts are-
1. inter- face
  Signature class. [*.h file]
2. implementation
Definition of a class.[*.m file]
3. Instantiation
instantiation of class by allocating memory.
It is a dynamic language. It supports open dynamic binding to create easy architecture for user interface.
Basic structure of a program-
#include <stdio.h>
//----Header file added------
int main(void)
        return ;
//----------Body Ends-----

Code file extension will be “.m”. It complies line by line. So if we need to call a function in other function, caller function should be bellow of called function.
#include <stdio.h>
-(void) called
-(void) caller
int main(void)
        return ;

For MAC ,Object compilation –
$ gcc -o hello hello.m \ -L /System/Library/Frameworks/Foundation.framework/Foundation
Run compiled code on MAC
$ ./hello
Compiler for Objective – c can be downloaded from - http://www.gnustep.org/experience/Windows.html. [For windows]

 Key Words----
used to declare of class or interface.
used to define a class or category.
used to declare a formal protocol.
ends the declaration, definition, category or protocol.
Limits the scope of an instance variable to the class that declares it.
Limits instance variable scope to declaring and inheriting classes.
Removes restrictions on the scope of instance variables.
Defines a block within which exceptions can be thrown.
Throws an exception object.
Catches an exception thrown within the preceding @try block.
A block of code that is executed whether exceptions were thrown or not in a @try block.
Declares the names of classes defined elsewhere.
It returns the compiled selector that identifies method_name.
Returns the protocol_name protocol (an instance of the Protocol class). (@protocol is also valid without (protocol_name) for forward
Yields a character string that encodes the type structure of type_spec.
Defines a constant NSString object in the current module and
initializes the object with the specified 7-bit ASCII-encoded string.
Defines a block of code that must be executed only by one thread
at a time.
It takes YES /NO


Example code –

#include <objc/Object.h>
@interface Car:Object
  //For instance variables 
- (void)company;
#include <stdio.h>
@implementation Car
- (void)company
#include <stdlib.h>
int main(void)
        id myCar;
        myCar =[Car new];
        [myCar company];
        [myCar free];
        return EXIT_SUCCESS;

Post a Comment