Tuesday, August 9, 2011

Xcode4: CSV Parser for iPhone/iPad iOS


I had to dig a while to find a Objective C CSV Parser I like and that has decent performance on a mobile device. One problem is that most of those parsers are originally written for Mac OS and they are not all easily transferable.

The cCSVParse from Michael Stapelberg is one of those lightweight CSV parsers. Since the website is entirely in German I will write a little summary and provide example for the English speakers on the Web.


1. Download the library

Either clone the project from Git.

Or download the archive from the CSV Parser Project Homepage. Scroll down to "Herunterladen" and click the link.

2. Copy the files in your project

Copy the parseCSV.h and parseCSV.m into your project. Make sure you have checked the copy function in Xcode.

3. Change the library for iOS use

Open the parseCSV.h and replace:

#import <Cocoa/Cocoa.h>


#import <UIKit/UIKit.h>

Example of how to use the CSV Parser

#import "parseCSV.h"


- (void)parseMyCSVFile{

     CSVParser *parser = [CSVParser new];
     //get the path to the file in your xcode project's resource path 
     NSString *csvFilePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"csv"];
     [parser openFile:csvFilePath];

     NSMutableArray *csvContent = [parser parseFile];
     for (int i = 0; i < [csvContent count]; i++) {
	NSLog(@"content of line %d: %@", i, [csvContent objectAtIndex:i]);
     [parser closeFile];


Post a Comment