Is .playground a swift file? Who can 'see' it?
A .playground file is actually a bundle directory, which can be verified by right clicking it and selecting "Show Package Contents".
It includes a .swift file for the actual source code, which can be edited normally , as well as a .xcplayground file and a .xctimeline file, which are opaque and can't be edited through normal means.
You can also add files to it, such as images and other resources to use inside your playground.
It's not necessary to be present in application projects. It's used solely as a place to explore, without the need to create a project.
Can Swift playgrounds see other source files in the same project?
They cannot. Playgrounds are self-contained. This will hopefully change in the future.
Edit: As of Xcode 6.3, Playgrounds can now contain supporting code. They still cannot see other code in the same project, but code can be added to the support folder of a Playground that can be used from within the playground. See the Swift blog for more info.
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)
Xcode playgrounds can't access swift files in Sources folder
You have to add public access attribute to your classes, methods and properties in source folder to make them accessible from main playground file as they treated as separate module by compiler
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!!
How do I open this chapter as a playground in Xcode?
- Launch Xcode 6
- Documentation and API Reference Window (command-shift-0)
- Search for “The Swift Programming Language”
- Click first suggestion
You will see a series of collapsable menus on the left side of the screen detailing different sections, such as “A Swift Tour”, and “Language Guide”. You can click through these to get different bits of information about different aspects of the language.
Many sections will feature the “Open Playground” links that you’ve described, except from here, they’re clickable. When you click one of these "Open Playground" links, the .playground
file associated with this section will be downloaded to your downloads folder, and can be directly opened with Xcode 6.
There is even another option that allows you to open the playground files directly without first dropping them in your downloads folder.
In the documentation viewer in Xcode 6, search for "A Swift Tour", or any other term that you find to yield you an article you're looking for:
Click a result that has a .playground icon to its left. From here, you can use the "Action" button on the right side of the window to open the file directly in an Xcode Playground.
Swift playgrounds with UIImage
- Open the .playground file in Finder.
- Create a folder called Resources next to it.
- Add any images you want to this folder.
- In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.
- Under 'Resource Path' choose 'Relative To Playground'
- Click the folder icon underneath and choose the Resources folder created earlier.
You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):
Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.
Related Topics
How to Synchronize Coredata with Webservices in Swift
Expression Resolves to an Unused Function
File Couldn't Be Opened Because You Don't Have Permission to View It Error
Why Can't I Use .Reduce() in a One-Liner Swift Closure with a Variadic, Anonymous Argument
Modifying an Array Passed as an Argument to a Function in Swift
Applewatch Messages Url Works Hard Coded But Not with Variables
How to Get Frame Data in Apprtc iOS App for Video Modifications
Xcode11 Error "Open(_:Options:Completionhandler:) Is Unavailable in Application Extensions"
JSONserialization.JSONobject Performance in Swift
Partial Application of 'Mutating' Method Is Not Allowed
Compile Time Key Path Checking in Swift
Running Swift Build in Terminal Leading to "Platform Path" Errors
Implemented Helper App But Does Not Launch on Login