Thursday, 16 August 2012

Memory Management

Memory Management :-

Depending on the type of application you’re writing, judicious use of memory can be
critical. For example, if you’re writing an interactive drawing application that creates
many objects during the execution of the program, you must take care that your program
doesn’t continue to consume more memory resources as it runs. In such cases, it becomes
your responsibility to intelligently manage those resources and free them when they’re no
longer needed.This means freeing resources during the program’s execution instead of
just waiting until the end.

NSAutorelease :- When dealing with a foundation programs you must setup this pool to use the foundation object.
                          
When the pool is  setup foundation automatically adds certain array,string etc when you are done using the pool you can release the memory is use by send its drain message [pool darain].
predefined classes.
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
In UserDefine Classes(autorelease):-

[objectname autorelease];


Release :- When you no longer needed an objects you dicrement its refrence count by ONE ,send its release message like [objectname release].

Reference Countinng:-
You can’t free up the memory an object uses until you are certain that everyone is done using that object.Luckily ,the foundation framework provides the elegant solution for keeping track of the number of refrences to an object .It involves a fairly straightforward thechnique calles a refrence counting.

When an object is create via a alloc method or  new keyworkd a copy of message its refrence count is set to ONE each time you needed to ensure that the object be kept arround you increment its refrence count by ONE sending its retain message.
[objtname retain]

Delloc :- When the refrence count object reach a ZERO, system knows that this object is no longer needed so it is free by its memory this done by message  delloc.
[objectname delloc]


Garbage Collection :- 
you have been creating your programs to run in a memorymanaged   
runtime environment.The memory-management rules summarized in the previous sections apply to such an  in environment,which you deal with autorelease pools, issues related to retaining and releasing objects, and object ownership.
 As of Objective C 2.0, an alternate form of memory management, known as garbage
collection, became available.With garbage collection, you don't have to worry about retaining
and releasing objects, autorelease pools, or retain counts. The system automatically
keeps tracks of what objects own what other objects, automatically freeing up (or garbagecollecting)
objects that are no longer referenced as space is needed during the program’s
execution.

Wednesday, 15 August 2012

Foundation Framework

Foundation Framework :-  A framework is collection of methods ,classes and documentation logically grouped together to make the developing easier.
                                                           The Foundation framework introduces several paradigms to avoid confusion in common situations, and to introduce a level of consistency across class hierarchies.

The Foundation class hierarchy is rooted in the Foundation framework’s NSObject class.

Cocoa Objective-C Hierarchy for Foundation :-

         












 Work in some classes  
NSNumber :-

Integer
NSNumber  *num1=[[NSnumber alloc]initWithValue:100];
NSInteger myInt;
myInt=[num IntergerValue];
NSLog(@"integer is =%@",myInt);

Char

myNumber = [NSNumber numberWithChar: ‘X’];
NSLog (@"%@",[myNumber  CharValue];

 Similar to the float/long/double.

Equal

if ([intNumber isEqualToNumber: floatNumber] == YES)
NSLog (@
”Numbers are equal”);
else
NSLog (@”Numbers are not equal”);


 NSString & NSMutableString :-

you created a character string object in Objective-C.The Foundation framework supports
a class called
NSString for working with character string objects.

NSString *str = @"Programming is fun";
NSLog (@”%@, str);


Ex:-
NSString *sh = @"This is string A";

NSString *sh1 = @This is string B;
NSString *sha";  


number of charactersNSLog (@Length of sh: %@, [sh length]);
Copy one string to another

sha = [NSString stringWithString: sh];
NSLog (@
copy: %@, sha);

Copy one string to the end of another

sh1 = [sh stringByAppendingString: sh1];
NSLog (@
Concatentation: %@, sh1);

Equal

if ([sh isEqualToString: sha] == YES)
NSLog (@ sh == sha);

else
NSLog (@ ”sh != res);


In Mutable string

[sh InsertString:@"hello"AtIndex:2];
[Sh InssertString @"end"AtIndex:[sh1 Length]]

NSArray & NSMUtableArray  :-

A Foundation array is an ordered collection of objects. Most often, elements in an array
are of one particular type, but that’s not required. Just as there are mutable and immutable
strings, are there mutable and immutable arrays.
Immutable arrays are handled by theNSArray class, whereas mutable ones are handled by NSMutableArray.

NSArayy *arr=[[NSArray alloc]initWithObjects:@"india",@"pak",@"uk",@''lanka''@"us",nil];
for(int i=0; i<=5, i++){
NSLog(@"%@",[arr AtIndex:i]);
}

MutableArray:-

NSMutable *arr=[[NSMutalbelArray alloc]init];
[arr addObject:@"india"];
[arr addObject”sh != res);
NSLog(@"%@",[arr ObjectAtIndex:1]);

[arr InsertObject :@"ram in" AtIndex:3]



NSDictionary & NSMutableDictionary :-

A dictionary is a collection of data consisting of key-object pairs.

NSm\MutableDictionary *obj =[NSMutableDictionary obj]
[obj SetObjects:@"indore" forKey:@"i"];
[obj SetObjects:@"kanpur" forKey:@"k"];
[obj SetObjects:@"nagpur" forKey:@"n"];

NSLog(@"%@",[obj ObjectKey:@"i"]);






Tuesday, 14 August 2012

Inheritance & Runtime polymorphism & Categories and Protocols

Inheritance:-
From a terminology point of view,we can speak of classes, child classes, and parent
classes.Analogously,we can talk about classes, subclasses, and superclasses.You should become
familiar with both types of terminology.
Whenever a new class (other than a new root class) is defined, the class inherits certain
properties. For example, all the instance variables and the methods from the parent implicitly
become part of the new class definition.That means the subclass can access these
methods and instance variables directly, as if they were defined directly within the class
definition.

Ex.
.h
@interface ClassA: NSObject
{
int x;
}
-(void) initVar;
@end
.m
@implementation ClassA
-(void) initVar
{
x = 100;
}
@end
add a other class
.h
@interface ClassB : ClassA
-(void) printVar
@end
.m
@implementation ClassB
-(void) printVar
{
NSLog (@
x = %d, x);
}
@end

Main Program
{

ClassB *b = [[ClassB alloc] init];
[b initVar]; 
[b printVar];
return 0;
}


1- Single inheritance

.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display1;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithphy:(float)phy andmath:(float)math andchem:(float)chem{
super=[super init];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);

} 
@end

Main Calss

#import "student.h"
#import"marks.h"

student *obj=[[student alloc]initWithrno:101 andbname:@"abc"];
marks *obj1=[[marks alloc]initWithphy:50 andmath:75 andchem:35];
[obj1 display];
[obj1 display1];
 @end

Method Overriding:-

.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem{


super=[super initWithrno:100 andbname:@"abc"];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
[super display];   // call a parent class method
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);
 }
@end

Main Program

# import "student.h"
#import"marks.h"

marks *obj=[[marks alloc]initWithname:@"abc" andrno:101 andphy:102 andmath:106 andchem:108];       
[obj dispaly];
@end

Runtime Polymorphism :-


.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem{


super=[super initWithrno:100 andbname:@"abc"];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
[super display];   // call a parent class method
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);
 }
@end

Main Program

# import "student.h"
#import"marks.h"

marks *obj=[[marks alloc]initWithname:@"abc" andrno:101 andphy:102 andmath:106 andchem:108];   
student *stu=[[student alloc]init]
stu=obj;    
[stu display];    //here called a chaild class method
@end


Categories  :- Some time you might  be working witha class defination want to add some new methods to it thats called a cataegories.
.h
@Interface student: NSObject{
NSString *name;
int rno;
float phy, chem,math total;
}
-(void)setrno:(int)r name:(NSString*)nm ;
-(void)show;
 @end
.m
@implementation
-(void)setrno:(int)r name:(NSString*)nm
{
rno=r;
name=nm;
}
-(void)show{
NSLog(@"roll no is=%d",rno);
NSLog(@"roll no is=%@",name);
}
@end

add a other calss:-
class name student-test
.h
#import"student.h"
@interface student(Test)
-(void)setphy:(float)ph  chem :(float)chem math :(float)math;
-(void)calculate;
-(void)display;
@end
.m 
@implementation
-(void)setphy:(float)ph  chem :(float)ch math :(float)ma{
phy=ph;
chem=ch;
math=ma;
}
-(void)calculate{

total=(phy+chem+math);
}
-(void)display{
NSLog(@"phy=%f",phy);
NSLog(@"chem=%f",chem);
NSLog(@"math=%f",math);
NSLog(@"total=%f",total);
}
@end

Main Program
#import "student.h"
#import"student-test.h"

student *obj =[student new];
[obj setrno:10 name:@"abc"];
[obj show];
[obj setphy:50  chem :60 math :55];
[obj calculate];
[obj display];
@end

Protocol :-A prottocol is a list of methods that is shatred a among classes .
if you have decides to implement all of the methods for a particular protocol your are said to confirm or adopt that protocol.

Note :- Protocol content only method decarlation and does not content a method defination

Syntax:-

@interface calss name:NSObject<protocol1, protocol2........>

.h
add there a protocol class and see that fomate
#import <foundation/foundation.h>
@Interface Display:NSObject {

}
-(void)display;
@end
and a other class demo
.h
#import"Display.h"
@interface demo:NSObject<Display>
{
}
.m
-(void)display
{
NSLog(@"Hello");

}
@end
Main program
#import"Display.h"
#import"Demo.h"

Demo *d=[demo new];
[d display];
@end

Property & Syntehsize

Function does not a return a multiple value(setter or getter) :-

.h
@interface product :NSObject{

NSString *pname;
int pcode;
float price;
}
-(void)setpname:(NSString*)pnm pcode:(int)pcd price :(float)pr;
-(void)getpname;
-(void)getpcode;
-(void)getprice;

@end

.m

@implementation-(void)setpname:(NSString*)pnm pcode:(int)pcd price :(float)pr{
pname=pnm;
pcode=pcd;
price=pr;
}
-(void)getpname{

return name;
}
-(void)getpcode
{
return pcode
}
-(void)getprice
{

return price;

}@end

Main program

#import "product.h"
product *obj =[product new];

[obj setpname:@"abc" pcode:101 price :101.1 ]
NSLog(@%@ pname is=,[getname]);
 NSLog(@"%d pcode is=",[getpcode]); 
NSLog(@%f price is=,[getprice]);
}
@end

Property & Syntehsize :-It is a keyword in a objective-c , its a request of a to compiler to generate a get and set methods for previously define property.


Syntax:-
.h
@property datatype variablename;
.m
@synthesize propertynamec ;

.h
@interface manager :NSObject{

int mcode;
NSString*mname;
float salary;
}
@property int mcode;
@property NSString *mname;
@property float salary;
@end

.m
@implemetation
@sysnthesize mcode,mname,salary;
@end

Main program
#import "manager.h"
manager*obj=[manager new];
obj.mcode=100;
obj.mname=@"shiv";
obj.salary=25000;

NSLog(@" manager code is=%d",mcode);
NSLog(@" manager name is=%@",mname);
NSLog(@" manager name is=%f",salary);
@end

Class ,Objects and Methods

Class   :- It is a collection of objects.
Object :- Object is thing.Think about object-oriented programming as a thing and something you want to do to that thigs.
 Ex.
  Object                                    What you do with it
 
    Bike                                      Wash it
                                                 Drive it
                                                 Service 
                                                 Fill it with petrol    

Object Create :-

1. Classname *Object =[[Classname allo]init];
2. Classname *Object= [Classname New];

Method Create :-

For single value:-

-(returntype)methodnamevariablename:(datatype)refrencevariable

For Maltible values:

-(returntype)methodnamevariablename:(datatype)refrencevariable   variablename1:(datatype)refrencevariable  variablename2:(datatype)refrencevariable  ;

With id:-

-(id)initWithvariablename:(datatype)variablename andvariablename:(datatype)variablename ;

Ex-
 .h

@interface person:NSObject{
NSString *name, address;
int contact;
}
//Method type ONE

-(void)setname:(NSString*)nm;
-(void)setcontact:(int)cn;
-(void)setaddress:(NSString)add;
-(void)display;

//Method type SECOND

-(void)setname:(NSString*)nm contact:(int)cn address:(NSStirng*)add;
-(void)display;

//Method type THIRD

-(id)initWithname:(NSString*)name andcontact:(int)contact andaddress:(NSString*)address;
-(void)display;

@end

.m


//Method type ONE

-(void)setname:(NSString*)nm
{
name=nm;
}
-(void)setcontact:(int)cn
{
conatct=cn;
}
-(void)setaddress:(NSString)add
{
address =add
}
-(void)display
{
NSLog(@"%@ name is=",name);
NSLog(@"%d conatct is=",conatct);
NSLog(@"%@ address is=",address);
}



//Method type SECOND

-(void)setname:(NSString*)nm contact:(int)cn address:(NSStirng*)add;
{
name=nm;
conatct=cn;
address=add;
}
-(void)display
{

NSLog(@"%@ name is="name);
NSLog(@"%d conatct is="conatct);
NSLog(@"%@ address is="address);

}

//Method type THIRD

-(id)initWithname:(NSString*)name andcontact:(int)contact andaddress:(NSString*)address
{
self=[super init];
if(self)
{
self->name=name;
self->contact=conatct;
self->address=address;


}

return self;

}
-(void)display
{
NSLog(@"%@ name is="name);
NSLog(@"%d conatct is="conatct);
NSLog(@"%@ address is="address);

}
@end

Main Program
#import "person.h"
//Method type for ONE
person * obj = [[person alloc]init];

[obj setname:@"abc"];
[obj setconatct:123454];
[obj setaddress:@"indore"];
[obj display];
@end


//Method type SECOND

 person * obj = [[person alloc]init];

[obj setname:@"abc" contact:12345 address:@"indore" ];
[obj display];
@end;

//Method type THIRD

person * obj = [[person alloc]initWithname:@"abc" andcontact:12345 andaddress:@"indore"];
[obj display];
@end




























  

Monday, 13 August 2012

History & Basic class

 Hi friends Hope are you well........

 Objective-C

 1. HISTORY-


*  1980 S : Objective-C language  designed by  Brand J.Cox and Tom Love at company  Stepstone .
                 
  –Object-oriented extension of C. Inspired by Smalltalk.
  –Strict superset of C.
        After Steve Jobs left Apple Inc. he started the company NeXT. 
*  1988 : NeXT Software licenses the Objective-C language and develops NEXTSTEP .
*  1992 : FSF adds Objective-C to GNU compiler suite .
*  1994 : NeXT Computer and Sun Microsystems release a standardized OPENSTEP specification
*    1996 : Apple acquires NeXT Software. Use for OS X. OPENSTEP now called Cocoa.


  In Xcode Editter Work in MVC architecture so in development conatined a mainly three sections:-

  1. Header files(.h).
  2.Implemenation files(.m).
  3.Programe file(Main method).

  Header file(.h) :-

  Its also called interface section , there define a variables and methods.

  Ex:-

  @Interface DemoClass : NSObject{                                  NSString - NS (NExtstep history reason)
                                                                                          srting declartaion like a pointer
  NSString *name;                                                                NSObject is a super class in fondation 
  int rno;                                                                                framework .(MM, and others)
  }

  -(void) setname:(NSString*)nm;
  -(void)setrno:(int)rn;
  -(void)show;
  @end

  Implementation section (.m):-

  There create a method body logical degine a programe in this section.
  #import "ClassDemo.h"

  @implementation ClssDemo

  -(void) setname:(NSString*)nm{
    name=nm;
  }
  -(void)setrno:(int)rn
  {
  rno=rn;

  }
  -(void)show

  {
  NSLog(@"%@ Name is",name);
  NSLog(@"%d rno is",rno);
  }
  @end

  Main Programe:-

  Object Creation.
  Methods Calling.

  ClassDemo *obj = [[ClassDemo allo]init];
  [obj setname:@"abe"];
  [obj setrno:101];
  [obj show];
  @end