Skip to content

Instantly share code, notes, and snippets.

@fitomad
Created February 26, 2018 11:06
Show Gist options
  • Save fitomad/27467f377cb01178760191a2e320087c to your computer and use it in GitHub Desktop.
Save fitomad/27467f377cb01178760191a2e320087c to your computer and use it in GitHub Desktop.
//
// 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