How to Read a File in a Swift Playground

How can I read a file in a swift playground

This works for me. The only thing I changed was to be explicit about the file name (which is implied in your example) - perhaps you have a typo in the off-screen definition of the "file" variable?

let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) as? [String]

let file = "trial.txt" // My change to your code - yours is presumably set off-screen
if let directories = dirs {
let dir = directories[0]; //documents directory
let path = dir.stringByAppendingPathComponent(file);

//read
let content = NSString(contentsOfFile: path, usedEncoding: nil, error: nil)
// works...
}

Update Swift 4.2

As @raistlin points out, this would now be

let dirs = NSSearchPathForDirectoriesInDomains(
FileManager.SearchPathDirectory.documentDirectory,
FileManager.SearchPathDomainMask.userDomainMask,
true)

or, more tersely:

let dirs = NSSearchPathForDirectoriesInDomains(.documentDirectory,
.userDomainMask, true)

How can I read a text file in the Resources directory using Swift 3 Playground?

Each Playground has its own Resources folder, and the actual path is hidden.

In Swift 3, you can get the files in the Resources folder by using Bundle:

let url = Bundle.main.url(forResource: "file", withExtension: "txt")

The path of this Bundle URL contains the path to the current Resources folder.


You can also avoid using Bundle and drag & drop the file from the Resources folder to the Playground itself, creating a file literal.

For example, type let url = and drop the file, from the Resources folder, into the Playground just after the = sign.

How to Read and Write data to a text file in Swift using playground?

After trying out for couple of days it is clear that,

We cannot access other than the project's self contained directories, if we try 'file handling' in playground. Because it is sandboxed

But from an xcode project, we can access any directory and perform read/write operations.

credits: @robMayoff

Swift Playground - Files are not readable

I have to thank Nate Cook for first for his quick response and solid answer.

Just for case I share his answer from another post, which title is misleading.

See Nate's answer here: https://stackoverflow.com/a/26723557/2360439

Playgrounds are sandboxed, so you won't be able to just grab files from anywhere in your user folder. Here's how to add that file to your
playground to make it accessible:

  • Find your ".playground" file in the Finder Right click and choose "Show Package Contents"
  • You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift"
  • Make a new folder called "Resources" if it doesn't exists yet.
  • Copy your files into Resources folder

Seems that there is no way to access files with Swift Playground outside of Playground sandbox. If you know how to access files outside of sandbox, you are welcome to share your solution!!

Show Package Contents

Resources Folder

Read plist in swift 3 playground

To finally get the program working I used.

devices = NSArray(contentsOfFile: path) as! [AnyObject]

The issue with playground continuously running was solved by going to activity monitor and force quitting the process named com.apple.coresimulator which was identified as not responding. After doing this the playground ran instantly.

How to access variables from Readline in a Playground

You can redirect the standard input to read from some file with

freopen("/path/to/input.txt", "r", stdin)

This works both in a Playground and in a compiled Command Line Tool project. If you want to keep the data in the Playground then

  • Open the project navigator (View -> Navigators -> Show Project Navigator (cmd-1)
  • Select the “Resources” folder and choose File -> New -> File (cmd-N).
  • Select the new file in the project navigator and rename it to “input.txt”.
  • Add the input data to this file.

In the Playground, redirect the standard input to read from the
resource file:

if let path = Bundle.main.path(forResource: "input", ofType: "txt") {
freopen(path, "r", stdin)
}

Now readLine() reads from “input.txt”.

How Do I import a local HTML file to my Swift Playground Live View?

Copy the HTML file to the Playground's "Resources" folder (go to "Navigator" > "Show project navigator" to display the folder).

Then use Bundle.main.url(forResource:) to fetch the file's URL, like this:

let u = Bundle.main.url(forResource: "Prac_1", withExtension: "html")


Related Topics



Leave a reply



Submit