Go to file
Yasuhiro Hatta cdeb78d3c0 Merge pull request #6 from yaslab/swift-3.0
Swift 3.0 support
2016-09-25 01:32:58 +09:00
CSV.xcodeproj Swift 3.0 support #2 2016-08-21 18:48:17 +09:00
Sources Bump version 1.0.0 2016-09-25 01:30:40 +09:00
Tests Swift 3.0 support #2 2016-08-21 18:48:17 +09:00
.gitignore Initial commit 2016-06-11 02:10:59 +09:00
.travis.yml Update .travis.yml 2016-08-21 18:52:04 +09:00
CSV.swift.podspec Bump version 1.0.0 2016-09-25 01:30:40 +09:00
LICENSE Initial commit 2016-06-11 02:10:59 +09:00
Package.swift Add Package.swift 2016-06-11 12:08:43 +09:00
README.md Bump version 1.0.0 2016-09-25 01:30:40 +09:00

README.md

CSV.swift

Build Status

CSV reading library written in Swift.

Usage

From CSV string

import CSV

for row in try! CSV(string: "1,foo\n2,bar") {
    print("\(row)")
    // => ["1", "foo"]
    // => ["2", "bar"]
}

From file path

import CSV

let stream = NSInputStream(fileAtPath: "/path/to/file.csv")!
for row in try! CSV(stream: stream) {
    print("\(row)")
}

Getting the header row

let csv = try! CSV(
    string: "id,name\n1,foo\n2,bar",
    hasHeaderRow: true) // default: false

let headerRow = csv.headerRow!
print("\(headerRow)") // => ["id", "name"]

for row in csv {
    print("\(row)")
    // => ["1", "foo"]
    // => ["2", "bar"]
}

Get the field value using subscript

let csv = try! CSV(
    string: "id,name\n1,foo",
    hasHeaderRow: true) // It must be true.

while csv.next() != nil {
    print("\(csv["id"]!)")   // => "1"
    print("\(csv["name"]!)") // => "foo"
}

Provide the character encoding

If you use a file path, you can provide the character encoding to initializer.

let csv = try! CSV(
    stream: NSInputStream(fileAtPath: "/path/to/file.csv")!,
    codecType: UTF16.self,
    endian: .Big)

Installation

CocoaPods

pod 'CSV.swift', '~> 1.0'

Carthage

github "yaslab/CSV.swift" ~> 1.0

Swift Package Manager

import PackageDescription

let package = Package(
    name: "PackageName",
    dependencies: [
        .Package(url: "https://github.com/yaslab/CSV.swift", majorVersion: 1, minor: 0)
    ]
)

Reference specification

License

CSV.swift is released under the MIT license. See the LICENSE file for more info.