Skip to content

Instantly share code, notes, and snippets.

@AndrewBennet
Created January 31, 2018 10:21
Show Gist options
  • Save AndrewBennet/4b98dba72072b5b3f4fc359afc5bcd0f to your computer and use it in GitHub Desktop.
Save AndrewBennet/4b98dba72072b5b3f4fc359afc5bcd0f to your computer and use it in GitHub Desktop.
"Fatal error: Index out of range" when both ',' and '\n' are within a quoted cell
import XCPlayground
import CSVImporter
class Row {
let cell1: String
let cell2: String
let cell3: String
init(cell1: String, cell2: String, cell3: String){
self.cell1 = cell1
self.cell2 = cell2
self.cell3 = cell3
}
}
let csvFileWithComma = "Column 1,Column 2,Column 3\n"
+ "cell 1.1,cell 1.2,cell 1.3\n"
+ "cell 2.1,\"cell 2.2, with comma\",cell 2.3"
let csvFileWithNewline = "Column 1,Column 2,Column 3\n"
+ "cell 1.1,cell 1.2,cell 1.3\n"
+ "cell 2.1,\"cell 2.2\n with newline\",cell 2.3"
let csvFileWithCommaAndNewline = "Column 1,Column 2,Column 3\n"
+ "cell 1.1,cell 1.2,cell 1.3\n"
+ "cell 2.1,\"cell 2.2, with comma and \n newline\",cell 2.3"
let importerWithComma = CSVImporter<Row>(contentString: csvFileWithComma)
importerWithComma.importRecords(structure: {print($0)}, recordMapper: {
return Row(cell1: $0["Column 1"]!, cell2: $0["Column 2"]!, cell3: $0["Column 3"]!)
})
let importerWithNewline = CSVImporter<Row>(contentString: csvFileWithNewline)
importerWithNewline.importRecords(structure: {print($0)}, recordMapper: {
return Row(cell1: $0["Column 1"]!, cell2: $0["Column 2"]!, cell3: $0["Column 3"]!)
})
let importerWithCommaAndNewline = CSVImporter<Row>(contentString: csvFileWithCommaAndNewline)
importerWithCommaAndNewline.importRecords(structure: {print($0)}, recordMapper: {
return Row(cell1: $0["Column 1"]!, cell2: $0["Column 2"]!, cell3: $0["Column 3"]!)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment