ESCSVParser is a type-safe CSV parser module for Swift 2.
You can write data type which use as a CSV row by simple code, and create an array of the data type from a CSV file easily.
This module is highly inspired by Himotoki which is a type-safe JSON decoding library !
struct StatisticalData : CSVRowRepresentable {
var country:String
var capital:String
var population:Int
var favolite:Bool?
static func fromRawLine(line: RawLine) throws -> StatisticalData {
return try line.make(StatisticalData.init)(from: 1, 2, 3, 4)
}
}
- Create a type which conforms to
CSVRowRepresentable
protocol. - Implement properties to the type. If the type of the property is an optional type, the property will assign
nil
if it meets certain condition (e.g. empty string toInt?
). - Implement an initializer if necessary. Normally use the default initializer.
- Implement
fromRawLine
method. In this method, you must create an instance of self type and return it. At this time, you can use raw csv line data which havingline
argument. By usingmake
method inline
, you can pick up raw columns by column index and convert to values of native type.
let csv = try! CSV<StatisticalData>(path: path)
- Use
CSV
type which specified RowType by generic parameter (e.g. CSV). - Call the initializer with CSV file path, you will get the data by an array of RowType.
let raw1:StatisticalData = csv[1]
XCTAssertEqual(raw1.country, "Japan")
XCTAssertEqual(raw1.population, 127767944)
- you can retrieve a row data in the CSV data by index which starting with 1.
- A csv file encoding by UTF-8.
- Columns are written in a natural text or a quoted text by double quotations.
Country,Capital,Population,favolite
Japan,Tokyo,127767944,yes
"U.S.A",Washington,"300007997",no
You can assign types that conforms to RawColumnConvertible
protocol to properties in a type for CSV row.
It is already conforms that types Int
, Int8
, Int16
, Int32
, Int64
, UInt
, UInt8
, UInt16
, UInt32
, UInt64
, String
, Double
, Float
, Float80
, Bool
and Optional<T>
which these type have.
You can make any CSV Column type using
RawColumnConvertible
.
If you want to other types, you will create a type which conforms to RawColumnConvertible
protocol. A type which conforms to this protocol and implement fromRawColumn
method can convert from a raw column value.
extension Bool : RawColumnConvertible {
public static func fromRawColumn(rawColumn: RawColumn) throws -> Bool {
switch rawColumn.value.lowercaseString {
case "true", "yes", "1":
return true
case "false", "no", "0":
return false
default:
throw FromRawColumnError()
}
}
}
It needs to throw
FromRawColumnError
if the type cannot convert fromrawColumn
value.
If you want to treat the type in Optional<T>
, the type will also need to conforms to RawColumnNullable
protocol. A type which conforms to this protocol and implement isRawColumnNull
method, you can specify the conditions which the value of the type will be convert to nil
.
extension Bool : RawColumnNullable {
static func isRawColumnNull(rawColumn:RawColumn) -> Bool {
return rawColumn.isEmpty
}
}
- Swift 2.1 (Xcode 7.1)
- iOS 8.0 or later / OS X 10.9 or later
Add following line to your Podfile and run pod install
.
pod 'ESCSVParser', :git => 'https://github.com/EZ-NET/ESCSVParser.git'
ESCSVParser is released under the MIT License.