I'm trying to write an array of numbers in a file. After reading it, the result is a string, I'm wondering how to read an array of numbers or transform the string into array of numbers?
let file = "sample.txt"
var arr1 = [Int]()
arr1 += 1...100
var text = String(describing: arr1)
var text1 : String = String()
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
let path = dir.appendingPathComponent(file)
// writing
do {
try text.write(to: path, atomically: false, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
//reading
do {
text1 = try String(contentsOf: path, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
Now the question is how can I transform text1 into an array of numbers?