Im trying to read a text file using a Swift playground with the following
let dirs : String[]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory
Swift 3 (Xcode 8)
The code below works in both iOS and macOS playgrounds. The text file ("MyText.txt" in this example) must be in the Resources
directory of the playground. (Note: You may need to open the navigator window to see the directory structure of your playground.)
import Foundation
if let fileURL = Bundle.main.url(forResource:"MyText", withExtension: "txt")
{
do {
let contents = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)
print(contents)
} catch {
print("Error: \(error.localizedDescription)")
}
} else {
print("No such file URL.")
}