Saturday, 13 June 2015

NSArray and NSDictionary in Objective C

NSArray and NSMutableArray in Objective C

If you have not read my last tutorial you can read from there:
Learn Objective C


Array is collection of similar  data type or arranged same type of data in linear form. Same data type means if you have array of integer you can not add other string or float values in an array.
In objective C NSArray class is used to provide memory space and other funtion for array.
In my last tutorial of init and id type: Click here

I told you when you use init during creation of object think it as constructor but actually it is not the constructor it is the initialisers. 
During that time i do not want you confuse with initFunction. But Real thing is that In Objective C there is no constructor only initializers.
When you go for enter data at run time then you need to use init method for checking super class is properly initialized or not.

In my case my class is inherited from NSObject so just for checking following code is used.

-(id)init
{
self=[super init];
if(!self)
{return null;
}
return self;
}
Otherwise you get error check for self.

Syntax to declare array:

NSArray *array=[[NSArray alloc]init];

NSArray class is used to assign same data type in linear or list form statically .No modification is possible. Otherwise you can get run time error. But you can also assign data  at run time for that NSMutableArray is used. The modification of data at run time is possible. Modification means insertion,deletion possible.




First go through NSArray class

In StudentName.h


interface StudentName : NSObject
{
    NSArray *studentname;
}
-(void)setStudentArray;

In Student.m

@implementation StudentName
-(void)setStudentArray
{
    NSString *studentname1=@"abc";
    NSString *studentname2=@"def";
    NSString *studentname3=@"ghi";
    NSString *studentname4=@"jkl";
    studentname=[NSArray arrayWithObjects:studentname1,studentname2,studentname3,studentname4 ,nil];
    for(int i=0;i<[studentname count];i++)
    {
        NSLog(@"The name of student is:%@",[studentname objectAtIndex:i]);
    }
}


Now go through NSMutableArray class. I have used both NSArray and NSMutableArray in one project you can use separate.It run also if you use NSArray class by creating its object or also for NSMutableArray.

In StudentNameDynamic.h

@interface StudentNameDynamic : NSObject
{
   NSMutableArray *studentname;
}
@property(nonatomic,strong)NSString *StudentName;
-(void)setStudentArray:(NSString *)StudentName;


In StudentNameDynamic.m


@implementation StudentNameDynamic
@synthesize StudentName=_StudentName;

-(id)init
{
    self=[super init];
    if(self)
    {
           studentname=[[NSMutableArray alloc]init];
    }
    return self;
        }
-(void)setStudentArray:(NSString *)StudentName
{

    [studentname addObject:StudentName];
    for (int i=0; i<[studentname count]; i++) {
        NSLog(@"The name of Student is:%@",[studentname objectAtIndex:i]);
    }
}

In main section


StudentName *student1=[[StudentName alloc]init];
        [student1 setStudentArray];
        StudentNameDynamic *studentrunttime1=[[StudentNameDynamic alloc]init];
    
        [studentrunttime1 setStudentArray:@"mno"];

Output is shown here:






Source Code Available here : Download Here

I hope you like my tutorial. If you have any query you can mail me or comment here.

Also there is assignment to you.For Simplicity i have pass the argument for NSMutableArray you can Enter its Value 
form the console run time.


My next tutorial will be NSDictionary


No comments:

Post a Comment