Created
February 26, 2018 11:06
-
-
Save fitomad/27467f377cb01178760191a2e320087c to your computer and use it in GitHub Desktop.
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
// | |
// New `joined` and `split` functions in Swift 4.1 | |
// | |
// More info at Swift [Array Documentacion](https://developer.apple.com/documentation/swift/array) | |
// | |
import Foundation | |
// The sample data... | |
let data: [String] = [ "Col1", "Col2", "Col3", "Col4", "Col5"] | |
// | |
// Export data to CSV format | |
// | |
let csv: String = data.joined(separator: ";") | |
// Result: Col1;Col2;Col3;Col4;Col5 | |
// | |
// Convert a string in an array | |
// | |
// map function in order to convert ArraySlice items to String | |
let backToData: [String] = csv.split(separator: ";").map(String.init) | |
// Result: ["Col1", "Col2", "Col3", "Col4", "Col5"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment