Create a gist now

Instantly share code, notes, and snippets.

import Foundation
let content = "abc,cde,5.5,128,256\nefg,ghi,42"
let parsedCSV: [[CustomStringConvertible]] = content
.components(separatedBy: "\n")
.map({ // Step 1
$0.components(separatedBy: ",")
.map({ // Step 2
if let int = Int($0) {
return int
} else if let double = Double($0) {
return double
}
return $0
})
})
print(parsedCSV)
// [["abc", "cde", 5.5, 128, 256], ["efg", "ghi", 42]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment