Swift Text File To Array of Strings
16,898
Solution 1
First you must read the file:
let text = String(contentsOfFile: someFile, encoding: NSUTF8StringEncoding, error: nil)
Then you separate it by line using the componentsSeparatedByString
method:
let lines : [String] = text.componentsSeparatedByString("\n")
Solution 2
Updated for Swift 3
var arrayOfStrings: [String]?
do {
// This solution assumes you've got the file in your bundle
if let path = Bundle.main.path(forResource: "YourTextFilename", ofType: "txt"){
let data = try String(contentsOfFile:path, encoding: String.Encoding.utf8)
arrayOfStrings = data.components(separatedBy: "\n")
print(arrayOfStrings)
}
} catch let err as NSError {
// do something with Error
print(err)
}
Solution 3
Updated for Swift 5:
The const path contains the file path.
do {
let path: String = "file.txt"
let file = try String(contentsOfFile: path)
let text: [String] = file.components(separatedBy: "\n")
} catch let error {
Swift.print("Fatal Error: \(error.localizedDescription)")
}
If you want to print what's inside of file.txt
line by line:
for line in text {
Swift.print(line)
}
Solution 4
Swift 4:
do {
let contents = try String(contentsOfFile: file, encoding: String.Encoding.utf8)
let lines : [String] = contents.components(separatedBy: "\n")
} catch let error as NSError {
print(error.localizedDescription)
}
Solution 5
Here is a way to convert a string to an array(Once you read in the text):
var myString = "Here is my string"
var myArray : [String] = myString.componentsSeparatedByString(" ")
This returns a string array with the following values: ["Here", "is", "my", "string"]
Related videos on Youtube
Author by
Henry oscannlain-miller
Updated on September 14, 2022Comments
-
Henry oscannlain-miller over 1 year
I was wondering what the simplest and cleanest to read a text file into an array of strings is in swift.
Text file:
line 1 line 2 line 3 line 4
Into an array like this:
var array = ["line 1","line 2","line 3","line 4"]
I would also like to know how to do a similar thing into struct like this:
Struct struct{ var name: String! var email: String! }
so take a text file and put it into struct's in an array.
Thanks for the help!
-
Suragch over 8 yearsSee this post for an excellent discussion on the options for converting a text file to a line array. codereview.stackexchange.com/a/100813/79551
-
-
Suragch over 8 yearsNote that some files do not just
\n
as the new line marker. You can consider usingNSCharacterSet.newlineCharacterSet()
andcomponentsSeparatedByCharactersInSet
. -
thumbtackthief over 8 yearsnew Swift syntax would be:
try! let text = String(contentsOfFile: someFile!, encoding: NSUTF8StringEncoding)
stackoverflow.com/questions/32663872/… -
esaruoho almost 5 yearsis there a version for swift5 out there somewhere? :)
-
esaruoho almost 5 yearsis there a version for swift5 out there somewhere? :)
-
esaruoho almost 5 yearsis there a version for swift5 out there somewhere? :)
-
esaruoho almost 5 yearsthanks muchly! :) and how would i be able to output that content into an Array? (one textfile, one Array)