Thursday, August 11, 2011

How to read from CSV file in iPhone code ?

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




3 comments:

  1. Hi, Thanks for the library.... but when i included it in my project and run the app i get this error and the graph display page is blank.... any idea? please help.... : CGPathAddArc: invalid value for start or end angle.

    ReplyDelete

  2. nice blogs.
    Friend chatMake new friend’s without revealing yourself. Dial 543216 at Rs 2/min and monthly subscription of Rs 30/ 30 days.

    ReplyDelete