[Swift] Parsing CSV with NTYCSVTable


Prepare cocoapods.

shell
$ gem install cocoapods
$ pod setup
$ vim Podfile
Podfile
platform :ios, "8.0"
pod 'NTYCSVTable'
shell
$ pod install
$ open YourProject.xcworkspace

Prepare "Bridging-Header.h"

By using "Bridging-Header", you can use "Objective-C" library from Swift in a few steps.

Create dummy "Objective-C" class file in project.

And then, *-Bridging-Header.h will be created.

Insert "NTYCSVTable" import header in this file.

YouyProject-Bridging-Header.h
#import "NTYCSVTable.h"
#import "NSString+NTYNonStringHandling.h"

After you get YouyProject-Bridging-Header.h, you can delete Dummy Objective-C file(.m) and header(.h).

Prepare sample CSV file

Now, I prepared Japanese prefecture sample csv here:

prefectures.csv
id,name
01,北海道
02,青森県
03,岩手県
04,宮城県
05,秋田県
06,山形県
07,福島県
08,茨城県
09,栃木県
10,群馬県
11,埼玉県
12,千葉県
13,東京都
14,神奈川県
15,新潟県
16,富山県
17,石川県
18,福井県
19,山梨県
20,長野県
21,岐阜県
22,静岡県
23,愛知県
24,三重県
25,滋賀県
26,京都府
27,大阪府
28,兵庫県
29,奈良県
30,和歌山県
31,鳥取県
32,島根県
33,岡山県
34,広島県
35,山口県
36,徳島県
37,香川県
38,愛媛県
39,高知県
40,福岡県
41,佐賀県
42,長崎県
43,熊本県
44,大分県
45,宮崎県
46,鹿児島県
47,沖縄県

and import this csv into Suppoting Files Directory.

Parse CSV!

Code like this:

ViewController.swift
    override func viewDidLoad() {
        super.viewDidLoad()

        testNTYCSVTable()
    }

    func testNTYCSVTable() {
        let csvPath = NSBundle.mainBundle().pathForResource("prefectures", ofType: "csv")
        let csvURL = NSURL.fileURLWithPath(csvPath)
        let table = NTYCSVTable(contentsOfURL: csvURL)

        var rows = table.rows
        println(rows)

        var headers = table.headers
        println(headers)

        var row_0 = table.rows[0] as Dictionary<String, AnyObject>
        println(row_0)

        var row_1 = table.rows[1] as Dictionary<String, AnyObject>
        println(row_1)

        var columns = table.columns
        println(columns)

        var names = table.columns["id"] as Array<Int>
        println(names)

        var ages = table.columns["name"] as Array<String>
        println(ages)   
    }

and then, you will get output in log.

log
[{
    id = 1;
    name = "\U5317\U6d77\U9053";
}, {
    id = 2;
    name = "\U9752\U68ee\U770c";
}, {
    id = 3;
    name = "\U5ca9\U624b\U770c";
}, {
    id = 4;
    name = "\U5bae\U57ce\U770c";
}, {
    id = 5;
    name = "\U79cb\U7530\U770c";
}, {
    id = 6;
    name = "\U5c71\U5f62\U770c";
}, {
    id = 7;
    name = "\U798f\U5cf6\U770c";
}, {
    id = 8;
    name = "\U8328\U57ce\U770c";
}, {
    id = 9;
    name = "\U6803\U6728\U770c";
}, {
    id = 10;
    name = "\U7fa4\U99ac\U770c";
}, {
    id = 11;
    name = "\U57fc\U7389\U770c";
}, {
    id = 12;
    name = "\U5343\U8449\U770c";
}, {
    id = 13;
    name = "\U6771\U4eac\U90fd";
}, {
    id = 14;
    name = "\U795e\U5948\U5ddd\U770c";
}, {
    id = 15;
    name = "\U65b0\U6f5f\U770c";
}, {
    id = 16;
    name = "\U5bcc\U5c71\U770c";
}, {
    id = 17;
    name = "\U77f3\U5ddd\U770c";
}, {
    id = 18;
    name = "\U798f\U4e95\U770c";
}, {
    id = 19;
    name = "\U5c71\U68a8\U770c";
}, {
    id = 20;
    name = "\U9577\U91ce\U770c";
}, {
    id = 21;
    name = "\U5c90\U961c\U770c";
}, {
    id = 22;
    name = "\U9759\U5ca1\U770c";
}, {
    id = 23;
    name = "\U611b\U77e5\U770c";
}, {
    id = 24;
    name = "\U4e09\U91cd\U770c";
}, {
    id = 25;
    name = "\U6ecb\U8cc0\U770c";
}, {
    id = 26;
    name = "\U4eac\U90fd\U5e9c";
}, {
    id = 27;
    name = "\U5927\U962a\U5e9c";
}, {
    id = 28;
    name = "\U5175\U5eab\U770c";
}, {
    id = 29;
    name = "\U5948\U826f\U770c";
}, {
    id = 30;
    name = "\U548c\U6b4c\U5c71\U770c";
}, {
    id = 31;
    name = "\U9ce5\U53d6\U770c";
}, {
    id = 32;
    name = "\U5cf6\U6839\U770c";
}, {
    id = 33;
    name = "\U5ca1\U5c71\U770c";
}, {
    id = 34;
    name = "\U5e83\U5cf6\U770c";
}, {
    id = 35;
    name = "\U5c71\U53e3\U770c";
}, {
    id = 36;
    name = "\U5fb3\U5cf6\U770c";
}, {
    id = 37;
    name = "\U9999\U5ddd\U770c";
}, {
    id = 38;
    name = "\U611b\U5a9b\U770c";
}, {
    id = 39;
    name = "\U9ad8\U77e5\U770c";
}, {
    id = 40;
    name = "\U798f\U5ca1\U770c";
}, {
    id = 41;
    name = "\U4f50\U8cc0\U770c";
}, {
    id = 42;
    name = "\U9577\U5d0e\U770c";
}, {
    id = 43;
    name = "\U718a\U672c\U770c";
}, {
    id = 44;
    name = "\U5927\U5206\U770c";
}, {
    id = 45;
    name = "\U5bae\U5d0e\U770c";
}, {
    id = 46;
    name = "\U9e7f\U5150\U5cf6\U770c";
}, {
    id = 47;
    name = "\U6c96\U7e04\U770c";
}]
[id, name]
[id: 1, name: 北海道]
[id: 2, name: 青森県]
{
    id =     (
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9,
        10,
        11,
        12,
        13,
        14,
        15,
        16,
        17,
        18,
        19,
        20,
        21,
        22,
        23,
        24,
        25,
        26,
        27,
        28,
        29,
        30,
        31,
        32,
        33,
        34,
        35,
        36,
        37,
        38,
        39,
        40,
        41,
        42,
        43,
        44,
        45,
        46,
        47
    );
    name =     (
        "\U5317\U6d77\U9053",
        "\U9752\U68ee\U770c",
        "\U5ca9\U624b\U770c",
        "\U5bae\U57ce\U770c",
        "\U79cb\U7530\U770c",
        "\U5c71\U5f62\U770c",
        "\U798f\U5cf6\U770c",
        "\U8328\U57ce\U770c",
        "\U6803\U6728\U770c",
        "\U7fa4\U99ac\U770c",
        "\U57fc\U7389\U770c",
        "\U5343\U8449\U770c",
        "\U6771\U4eac\U90fd",
        "\U795e\U5948\U5ddd\U770c",
        "\U65b0\U6f5f\U770c",
        "\U5bcc\U5c71\U770c",
        "\U77f3\U5ddd\U770c",
        "\U798f\U4e95\U770c",
        "\U5c71\U68a8\U770c",
        "\U9577\U91ce\U770c",
        "\U5c90\U961c\U770c",
        "\U9759\U5ca1\U770c",
        "\U611b\U77e5\U770c",
        "\U4e09\U91cd\U770c",
        "\U6ecb\U8cc0\U770c",
        "\U4eac\U90fd\U5e9c",
        "\U5927\U962a\U5e9c",
        "\U5175\U5eab\U770c",
        "\U5948\U826f\U770c",
        "\U548c\U6b4c\U5c71\U770c",
        "\U9ce5\U53d6\U770c",
        "\U5cf6\U6839\U770c",
        "\U5ca1\U5c71\U770c",
        "\U5e83\U5cf6\U770c",
        "\U5c71\U53e3\U770c",
        "\U5fb3\U5cf6\U770c",
        "\U9999\U5ddd\U770c",
        "\U611b\U5a9b\U770c",
        "\U9ad8\U77e5\U770c",
        "\U798f\U5ca1\U770c",
        "\U4f50\U8cc0\U770c",
        "\U9577\U5d0e\U770c",
        "\U718a\U672c\U770c",
        "\U5927\U5206\U770c",
        "\U5bae\U5d0e\U770c",
        "\U9e7f\U5150\U5cf6\U770c",
        "\U6c96\U7e04\U770c"
    );
}
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]
[北海道, 青森県, 岩手県, 宮城県, 秋田県, 山形県, 福島県, 茨城県, 栃木県, 群馬県, 埼玉県, 千葉県, 東京都, 神奈川県, 新潟県, 富山県, 石川県, 福井県, 山梨県, 長野県, 岐阜県, 静岡県, 愛知県, 三重県, 滋賀県, 京都府, 大阪府, 兵庫県, 奈良県, 和歌山県, 鳥取県, 島根県, 岡山県, 広島県, 山口県, 徳島県, 香川県, 愛媛県, 高知県, 福岡県, 佐賀県, 長崎県, 熊本県, 大分県, 宮崎県, 鹿児島県, 沖縄県]