@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 = [[NSArray alloc]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 alloc] init];
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
- Accept data from user.
- Display the data accepted.
- Replace data at a particular index.
- Delete a data from a particular index.
@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:[NSString stringWithCString: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 alloc] init];
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.
it really helpful to understand diffrance between nsarray & nsmutable array. Thankx
ReplyDeleteAmijeet
dats g8.its really helpful 4 me .thanks radix
ReplyDeletethis is very helpful for me to judge difference between NSArray & NSMutableArray,thanks Radix.
ReplyDeleteThats great,its really helpful,thanks ravi
ReplyDeleteThanx ravi it's use ful for me
ReplyDeleteas i am reading your blog...it generates more interest..thank u Ravi Sir....
ReplyDeleteIt is really helpful for new peoples also
ReplyDelete