How to read from CSV file in iPhone code ?

H

 

So you exported your data as csv file, and now say you want to make your piechart reading the data from csv file. Then you need to read the csv file. Well, its simple !

 

So our csv file looks like following and we will read say 1st column of each line.

 

 

 

 

 

 

 

 

 

 

 

 

We will call method with column value 0, you can change the value to 1,2, 3 or 4 …. to read subsequent columns as per your requirement:

 

 

[self readTitleFromCSV:csvPath  AtColumn:0];

 

And  our method looks like following:

 

 

-(void)readTitleFromCSV:(NSString*)path AtColumn:(int)column

{

 

 

titleArray=[[NSMutableArray alloc]init];

 

NSString *fileDataString=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

NSArray *linesArray=[fileDataString componentsSeparatedByString:@”n”];

 

 

int k=0;

for (id string in linesArray)

if(k<[linesArray count]-1){

 

NSString *lineString=[linesArray objectAtIndex:k];

NSArray *columnArray=[lineString componentsSeparatedByString:@”;”];

[titleArray addObject:[columnArray objectAtIndex:column]];

k++;

 

}

 

NSLog(@”%@”,titleArray);

 

}

 

Output of titleArray looks like following

 

 

 

 

 

About the author

mayur.kore
By mayur.kore

Category