Created
November 3, 2016 17:43
-
-
Save Dev1an/8c5d1a2e7e4274238e404522d2f606fb to your computer and use it in GitHub Desktop.
import CSV in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol CommaSeparatedValueCompatible { | |
init(values: [String]) throws | |
} | |
public func arrayFromCSV<Record: CommaSeparatedValueCompatible>(at file: URL, lineSeparator: String = "\n", columnSeparator: String = ",") throws -> [Record] { | |
let lines = try String(contentsOf: file).trimmingCharacters(in: CharacterSet(charactersIn: lineSeparator)).components(separatedBy: lineSeparator) | |
return try lines.dropFirst().map { line in | |
return try Record(values: line.components(separatedBy: columnSeparator)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment