Skip to content

CSV parser for Objective-C inspired by CSV::Table class of Ruby.

License

Notifications You must be signed in to change notification settings

marvin-yorke/NTYCSVTable

 
 

Repository files navigation

NTYCSVTable

Version Platform Build Status

Installation

NTYCSVTable is available through CocoaPods, to install it simply add the following line to your Podfile:

platform :ios
pod "NTYCSVTable"

Usage

For example, if you want to parse a below users.csv,

id,name,age
1,Alice,18
2,Bob,19
3,Charlie,20

you can access data by rows and columns like this.

NSURL *csvURL = [NSURL URLWithString:@"users.csv"];
NTYCSVTable *table = [[NTYCSVTable alloc] initWithContentsOfURL:csvURL];

// Rows
NSArray *rows = table.rows;
NSArray *headers = table.headers;    //=> @[@"id", @"name", @"age"]
NSDictionary *alice = table.rows[0]; //=> @{@"id": @1, @"name": @"Alice", @"age": @18}
NSDictionary *bob = table.rows[1];   //=> @{@"id": @2, @"name": @"Bob", @"age": @19}

// Columns
NSDictionary *columns = table.columns;
NSArray *names = table.columns[@"name"]; //=> @[@"Alice", @"Bob", @"Charlie"]
NSArray *ages = table.columns[@"age"];   //=> @[@18, @19, @20]

Also, you can find rows which have a specified value for a specified header like this.

[table rowsOfValue:@1 forHeader:@"id"];  //=> @[@{@"id": @1, @"name": @"Alice", @"age": @18}]
[table rowsOfValue:@20 forHeader:@"age"] //=> @[@{@"id": @3, @"name": @"Charlie", @"age": @20}]

Other formats

You can parse other formats such as TSV by using initWithContentsOfURL:columnSeparator:.

NSURL *tsvURL = [NSURL URLWithString:@"users.tsv"];
NTYCSVTable *table = [[NTYCSVTable alloc] initWithContentsOfURL:tsvURL columnSeparator:@"\t"];

About

CSV parser for Objective-C inspired by CSV::Table class of Ruby.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 92.1%
  • Ruby 7.9%