If you go to the menu
View → Debug Area → Show Debug Area
you will see a complete error: "you do not have access rights to the file system from the playground".
The workaround is to include the file in the Playground using its Project Navigator.
Go to menu
View → Navigators → Show Project Navigator
then drag your file into the Resources folder.
Then use the NSBundle to get the path.
func testFileLoad() { // get the file path for the file from the Playground Resources folder guard let path = NSBundle.mainBundle().pathForResource("test", ofType: "txt") else { print("Oops, the file is not in the Playground") return } // keeping the examples from your question let s: String = try! String(contentsOfFile: path, encoding: NSUTF8StringEncoding) print(s) do { let p: String = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding) print(p) } catch { print("nope") } } testFileLoad()
* In fact, you only have access to the /var/ folder containing your shared data on the Playground, and Playground just offers a shortcut. This folder in the Playground navigator actually represents the /var/ folder and is unique for each playground. You can see its address using NSBundle:
NSBundle.mainBundle().resourcePath
source share