Saturday, April 14, 2012

NSArray and NSMutableArray

In Objective C 2.0 we have two classes called as NSArray and other as NSMutableArray, the first question that came into my mind was:

Why are their two classes for Arrays???

Then i found the answer that the objects that you put in NSArray cannot be changed, i.e the size of NSArray does not change and its totally the reverse case in NSMutableArray. This is the reason why in most of the programming for iPhone, developers use NSMutableArray and not NSArray, NSMutableArray are like the array list of Java and C#.
So in my first example i will show you how to display data from NSArray and in my second example i will be using an NSMutableArray to accept the data from the user and display it into the console, so lets have a look at NSArray part first

Open the Xcode IDE and select command line utility and save your project by giving it an appropriate name.

First i have made a class called as Myclass in which i have a object of NSArray which is globally declared and a method where i will be dsiplaying data from the object of NSArray

@interface Myclass : NSObject
{
NSArray *arr;  //global NSArray object
}

-(void)NSArray_Demo;

@end

Now as my declaration part is completed no i will be implementing Myclass using the keyword implementation

@implementation Myclass

-(void)NSArray_Demo
{
arr = [[NSArrayalloc]initWithObjects:@"Ravi",@"Riki",@"Faizal",nil];

// here nil indicates the end of array elements 
for(int i =0;i<[arr count];i++)
{
NSLog(@"%@",[arr objectAtIndex:i]);
}
[arr release]; //release arr.


@end


Now the final touch

Guessed it right the main method

int main (int argc, const char * argv[]) {

    NSAutoreleasePool * pool = [[NSAutoreleasePool allocinit];

Myclass *obj = [[Myclass alloc]init];
[obj NSArray_Demo];
[pool drain];
    return 0;
}

Run the application by pressing Run from the meny bar and select console and then Build and Go.

The entire code looks like this 


NSMutableArray:

If you want to add, remove or replace data present in array then use NSMutableArray.
For NSMutableArray we will see the following
  1. Accept data from user.
  2. Display the data accepted.
  3. Replace data at a particular index.
  4. Delete a data from a particular index.
As we did in the case of NSArray we will make a class for NSMutableArray examples so that you can get a clear picture, so lets begin

@interface MyMutablearray : NSObject
{
NSMutableArray *Marr;
}

-(void)getdata;
-(void)replacedata;
-(void)displaydata;
-(void)deletedata;


@end



Now we have finished with the declaration part now lets see the implementation part

@implementation MyMutablearray

-(void)getdata
{
Marr = [[NSMutableArray alloc]initWithCapacity:3]; // initialize the array with capacity

//You can also initialize the mutable array just like you did in the case of NSArray in the above example

char chr[10]; //declare a character array
for(int i =0;i<3;i++)
{
scanf("%s",chr);//accept the input from the user
[Marr addObject:[NSStringstringWithCString:chr]]; //typecast the c array and add it to the mutablearray
}
}

//The data present at index 0 will be replaced by the new object in this function
-(void)replacedata
{
[Marr replaceObjectAtIndex:0 withObject:@"Ravi"];
}



// This function will display the data present from Mutable array and will show it on the console.

-(void)displaydata
{
for(int i =0;i<[Marr count];i++)
{
NSLog(@"%@",[Marr objectAtIndex:i]);
}
}

//delete a particular index
-(void)deletedata
{
[Marr removeObjectAtIndex:0];
}

@end

Now lets make the object of our class and execute the program so hope into your main method

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool allocinit];
MyMutablearray *obj = [[MyMutablearray alloc]init];
[obj getdata];
[obj displaydata];
[obj deletedata];
NSLog(@"Calling displaydata function after deletedata");
[obj displaydata];
[pool drain];
    return 0;
}

after execution you will get the result as shown in the image.



No comments:

Post a Comment