Skip to content

Instantly share code, notes, and snippets.

@algal
Created October 28, 2016 18:59
Show Gist options
  • Save algal/aad06d2c78f7be01f901af5f18e1ccba to your computer and use it in GitHub Desktop.
Save algal/aad06d2c78f7be01f901af5f18e1ccba to your computer and use it in GitHub Desktop.
// known-good. Xcode 8, Swift 3
import XCPlayground
/// writes string s to a file in the playground shared directory and returns its URL
func write(string s:String, toPlaygroundSharedDataDirectoryFileWithName name:String) -> URL?
{
let dirURL:URL = XCPlaygroundSharedDataDirectoryURL as URL
let fileURL:URL = dirURL.appendingPathComponent(name)
let path = fileURL.path
FileManager.default.createFile(atPath: path, contents: nil, attributes: nil)
guard let handle = FileHandle(forWritingAtPath: path) else { return nil }
defer { handle.closeFile() }
let data:Data = s.data(using: .utf8)!
handle.write(data)
return fileURL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment