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)
{
        printf("Hello!");
        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
{
        called();
}
int main(void)
{
        caller();
        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----
@interface
used to declare of class or interface.
@implementation
used to define a class or category.
@protocol
used to declare a formal protocol.
@end
ends the declaration, definition, category or protocol.
@private
Limits the scope of an instance variable to the class that declares it.
@protected
Limits instance variable scope to declaring and inheriting classes.
@public
Removes restrictions on the scope of instance variables.
@try
Defines a block within which exceptions can be thrown.
@throw
Throws an exception object.
@catch
Catches an exception thrown within the preceding @try block.
@finally
A block of code that is executed whether exceptions were thrown or not in a @try block.
@class
Declares the names of classes defined elsewhere.
@selector(method_name)
It returns the compiled selector that identifies method_name.
@protocol(protocol_name)
Returns the protocol_name protocol (an instance of the Protocol class). (@protocol is also valid without (protocol_name) for forward
declarations.)
@encode(type_spec)
Yields a character string that encodes the type structure of type_spec.
@"string"
Defines a constant NSString object in the current module and
initializes the object with the specified 7-bit ASCII-encoded string.
@synchronized()
Defines a block of code that must be executed only by one thread
at a time.
bool
It takes YES /NO
'self'

‘Super’



Example code –

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

Post a Comment